我有以下XAML代码:如何在这种情况下找到ComboBox值?
<ComboBox Name="cmbColors">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle Fill="{Binding Name}" Width="16" Height="16" Margin="0,2,5,2" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
和的.cs代码:
public ComboBoxDataBindingSample()
{
InitializeComponent();
cmbColors.ItemsSource = typeof(Colors).GetProperties();
}
什么是一种方法,我可以设置显示为“黑”当前组合框的值,而无需使用一个for循环找到它的索引?
请记住这个组合框包含一个矩形和一个文本块。做cmbColors.SelectedItem =“黑色”;不会将所选项目设置为黑色。 – John