C# WPF组合框不显示所选内容
情景: MyControl类型的用户控件列表: 问题是组合框正确显示了项目,但当我选择一个项目时,它不会出现在组合框中。为什么?C# WPF组合框不显示所选内容,c#,wpf,combobox,C#,Wpf,Combobox,情景: MyControl类型的用户控件列表: 问题是组合框正确显示了项目,但当我选择一个项目时,它不会出现在组合框中。为什么? PS:HeaderTitle是MyControl类型的从属属性。尝试这样绑定它 <ComboBox ItemsSource="{Binding Controls}" SelectedItem="{Binding SelectedControl, Mode=TwoWay}" DisplayMemberPath="{Binding HeaderTitle}" >
PS:HeaderTitle是MyControl类型的从属属性。尝试这样绑定它
<ComboBox ItemsSource="{Binding Controls}" SelectedItem="{Binding SelectedControl, Mode=TwoWay}" DisplayMemberPath="{Binding HeaderTitle}" >
您不需要将所选属性绑定到其类。。。它也可以是一个字符串。因此,只需将选定项存储在类型字符串中,然后从列表中获取与该选定项匹配的项。是否尝试设置组合框的数据上下文
<ComboBox DataContext="{Binding Controls}" ItemsSource="{Binding Controls}" DisplayMemberPath="HeaderTitle">
只要设置了ItemsSource和DisplayMemberPath,就不必绑定SelectedItem属性。我认为这是一个与 因此,我想引用亨氏K的答案 我也遇到了同样的问题,通过重写CustomObject中的Equals方法并比较Id属性来解决 如果选定的项与列表中包含的实例不同,则必须覆盖CustomObject中的Equals,以让ComboBox知道它是同一个对象 如果是同一个实例,可能只是简单的事情,例如将BindingMode设置为TwoWay:
你提出的替代方案就是我现在正在做的。但我很想知道为什么第一种方法不起作用……MyControl中有哪些项目?事实上,我也觉得这是对的。如果将DataContext设置为控件,则无法将ItemsSource设置为控件…可以为ComboBox所属的容器设置DataContext吗?您可以发布HeaderTitle属性的定义以及如何将MyControl对象填充到列表中吗?SelectedItem属性不同。它从不用于在组合框中显示项目。是否尝试从代码设置它?因为在这种情况下,您需要实现INotifyPropertyChanged,并在设置SelectedControl属性时引发PropertyChanged事件。我正在使用MVVM模式。INotifyPropertyChanged得到了很好的实现。
<ComboBox ItemsSource="{Binding Controls}" SelectedItem="{Binding SelectedControl, Mode=TwoWay}" DisplayMemberPath="{Binding HeaderTitle}" >
<ComboBox DataContext="{Binding Controls}" ItemsSource="{Binding Controls}" DisplayMemberPath="HeaderTitle">
SelectedItem="{Binding Path=CustomSettingProperty,Mode=TwoWay}"