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