2010-04-15 67 views
0

我在ListView中遇到了Combobox问题。 我有一个叫“Substrate”的课。这个类包含一个名为“SubstrateType”的类的对象。我想在Listview中显示类“Substrate”的对象。 “Substrate”的每个属性都显示在Listview的列中。对于不同的“SubstrateType”,我想使用组合框。Wpf SelectedItem不会为ListView中的Combobox工作

在XAML我试着这样说:

<ComboBox Name="CBType" 
      ItemsSource="{Binding ElementName=SettingsSubstrate, Path=TypeList}" 
      SelectedItem="{Binding Path=Substrate.SubstrateType}" 
      DisplayMemberPath="Description"/> 

在代码隐藏我有两个ObservableCollections。一个用于所有衬底,另一个用于所有可能的衬底类型。如果单击它,组合框将显示所有SubstrateType。但是如果你不选择一个,组合框没有selecteditem。

http://i44.tinypic.com/2eakxav.png

在此先感谢。

回答

0

我不知道你确切的代码,但如果你的ListView行显示Substrate类型的对象,那么对于SelectedItem您绑定路径应该只是SubstrateType因为一个ListViewItem的DataContext的已被设置为Substrate对象:

SelectedItem="{Binding Path=SubstrateType}" 

此外,您需要确保您的SubstrateType实际上被认为是平等的。如果Substrate对象中的SubstrateType实例不是,而TypeList属性中的实例相同,将不会被选中。您可以通过覆盖Equals(...)方法并定义您的自定义比较来确定相等性。

如果这不起作用,请提供更多代码,例如周围的XAML和代码Substrate以及code-behind/ViewModel/whatever。