C# ComboBox ItemSource ObservationCollection
我有一个组合框,其中包含绑定到观测集合的项源,并使用数据模板作为项模板。当我点击组合框时,我遇到了一个问题。值,并且它没有在组合框中显示所选值的值 以下是我的数据模板:C# ComboBox ItemSource ObservationCollection,c#,wpf,combobox,C#,Wpf,Combobox,我有一个组合框,其中包含绑定到观测集合的项源,并使用数据模板作为项模板。当我点击组合框时,我遇到了一个问题。值,并且它没有在组合框中显示所选值的值 以下是我的数据模板: <DataTemplate x:Key="ComboBoxTemplate"> <StackPanel Orientation="Horizontal"> <Rectangle Fill="{Binding ColorCode}" Wi
<DataTemplate x:Key="ComboBoxTemplate">
<StackPanel Orientation="Horizontal">
<Rectangle Fill="{Binding ColorCode}" Width="30" Height="15"/>
<TextBlock Text="{Binding ColorName}" Margin="5,0,0,0"/>
</StackPanel>
</DataTemplate>
下面是我的组合框:
<ComboBox Name="cmbAccentColors" Grid.Column="1" Width="130" Height="20"
ItemsSource="{Binding Source={StaticResource ComboColorData}}"
ItemTemplate="{StaticResource ComboBoxTemplate}"
IsSynchronizedWithCurrentItem="True" MaxDropDownHeight="120"
SelectionChanged="cmbColors_SelectionChanged"
>
Private void cmbColors_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (!IsLoaded)
{
return;
}
MessageBox.Show(cmbAccentColors.SelectedItem.ToString());
}
私有无效cmbColors\u SelectionChanged(对象发送者,System.Windows.Controls.SelectionChangedEventArgs e)
{
如果(!已加载)
{
回来
}
Show(cmbancencolors.SelectedItem.ToString());
}
将DisplayMemberPath添加到组合框中。DisplayMemberPath指定路径
DisplayMemberPath=“YourProperty”
这也是您访问所选项目的方式
Private void cmbColors_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
string text = (sender as ComboBox).SelectedItem.Text;
}
但是它说不能同时设置DisplayMemberPath和ItemTemplate。@user3639591为什么需要设置ItemTemplate=“{StaticResource ComboxTemplate}”@Sajeeetharan,因为我有矩形的颜色。在组合框内。。组合框中有矩形和文本块。