C# WPF组合框绑定到字典-返回的值错误
我有一个非常简单的用例: 带有C# WPF组合框绑定到字典-返回的值错误,c#,wpf,combobox,C#,Wpf,Combobox,我有一个非常简单的用例: 带有NotifyPropertyChanged()事件的属性,带有一些静态数据和组合框的字典 CB的定义如下: <ComboBox ItemsSource="{Binding AllThings}" DisplayMemberPath="Value" SelectedValuePath="Key" SelectedItem="{Binding Thing, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged
NotifyPropertyChanged()
事件的属性,带有一些静态数据和组合框的字典
CB的定义如下:
<ComboBox ItemsSource="{Binding AllThings}" DisplayMemberPath="Value"
SelectedValuePath="Key" SelectedItem="{Binding Thing, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}">
虚拟机包含:
public Dictionary<string, string> AllThings { get; set; }= new Dictionary<string, string>{["a"] = "b"};
private string thing;
public string Thing
{
get
{
return this.thing;
}
set
{
if (this.thing != value)
{
this.thing = value;
this.OnPropertyChanged();
}
}
}
公共字典AllThings{get;set;}=newdictionary{[“a”]=“b”};
私密的东西;
公共字符串
{
收到
{
归还这个东西;
}
设置
{
如果(this.thing!=值)
{
这个东西=价值;
this.OnPropertyChanged();
}
}
}
当用户选择该值时,我得到的不是“b”,而是一个奇怪的字符串:
[a,b]
在设置器内的值中
我想显示“b”并在“Thing”中存储“a”
UPD同样,将对象设置为b
不会导致列表中选择b
。使用SelectedValue而不是SelectedItem。该项是字典中的键/值对,它不是您想要的