WPF - Combobox SelectedItem not getting set?

I have a ComboBox that has its ItemsSource bound to a static List of options. The ComboBox is part of a form which is bound to a CustomObject class, and one of the properties on that class is a CustomSettingProperty.

I would like to bind the SelectedItem of the ComboBox to the property specified in the CustomObject, however SelectedItem="{Binding Path=CustomSettingProperty}" is not setting the default selected item. Using breakpoints I can see that it is calling the get; method, so I think the problem might be in the fact the CustomSettingProperty is created separately from the List so WPF does not think it is the same item.

Is there an easy way to do this? Or perhaps an alternative since the CustomSettings class does contain an Id?

11
задан Rachel 17 August 2010 в 19:29
поделиться

1 ответ

Если выбранный элемент не совпадает с экземпляром, содержащимся в List, вы должны переопределить Equals () в CustomObject, чтобы ComboBox знал, что это тот же объект.

Если это один и тот же экземпляр, возможно, это всего лишь простая вещь, например установка BindingMode на TwoWay:

SelectedItem="{Binding Path=CustomSettingProperty,Mode=TwoWay}"
22
ответ дан 3 December 2019 в 05:11
поделиться
Другие вопросы по тегам:

Похожие вопросы: