Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 组合框WPF模板项和选定项_C#_Wpf_Xaml - Fatal编程技术网

C# 组合框WPF模板项和选定项

C# 组合框WPF模板项和选定项,c#,wpf,xaml,C#,Wpf,Xaml,我尝试为组合框创建一个模板。ItemTemplate正常,但不是所选项目,如下所示: 我的代码: <ComboBox ItemsSource="{Binding CouleursList}" SelectedItem="{Binding SelectedCouleur}" Grid.Column="1" Grid.Row="2">

我尝试为组合框创建一个模板。ItemTemplate正常,但不是所选项目,如下所示:

我的代码:

                <ComboBox ItemsSource="{Binding CouleursList}" 
                      SelectedItem="{Binding SelectedCouleur}" 
                      Grid.Column="1" Grid.Row="2">
                <ComboBox.ItemContainerStyle>
                    <Style TargetType="{x:Type ComboBoxItem}">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    </Style>
                </ComboBox.ItemContainerStyle>
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Rectangle Stroke="Black" Margin="1" Height="15"
                                       HorizontalAlignment="Stretch">
                                <Rectangle.Fill>
                                    <SolidColorBrush Color="{Binding Path=., Converter={StaticResource ColorConverter}}"/>
                                </Rectangle.Fill>
                            </Rectangle>
                        </Grid>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

如果我给了一个固定的宽度,这是可行的,但矩形是左对齐和列表的中心


谢谢

所选项目内容不会显示在
ComboBoxItem
中,因此您的
HorizontalContentAlignment
样式设置程序将不适用

您可以在组合框本身上设置该属性,但是:

<ComboBox 
    ItemsSource="{Binding CouleursList}" 
    SelectedItem="{Binding SelectedCouleur}"
    HorizontalContentAlignment="Stretch"