Binding a WPF ComboBox to a custom list

后端 未结 4 560
暗喜
暗喜 2020-11-22 11:59

I have a ComboBox that doesn\'t seem to update the SelectedItem/SelectedValue.

The ComboBox ItemsSource is bound to a property on a ViewModel class that lists a bunch

4条回答
  •  太阳男子
    2020-11-22 12:35

    I had a similar issue where the SelectedItem never got updated.

    My problem was that the selected item was not the same instance as the item contained in the list. So I simply had to override the Equals() method in my MyCustomObject and compare the IDs of those two instances to tell the ComboBox that it's the same object.

    public override bool Equals(object obj)
    {
        return this.Id == (obj as MyCustomObject).Id;
    }
    

提交回复
热议问题