Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF组合框绑定到字典-返回的值错误_C#_Wpf_Combobox - Fatal编程技术网

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。该项是字典中的键/值对,它不是您想要的