WPF controls, disable changes to `SelectedItem` when bound property to `ItemsSource` changes

一笑奈何 提交于 2020-01-07 04:07:10

问题


When I use ComboBox or other controls that have ItemsSource and SelectedItem property bindings then each time upon the initial binding during runtime and also each time when the bound collection to ItemsSource changes I experience that the content of bound SelectedItem object is changed.

How can I disable this?

For example:

I have <ComboBox MinWidth="300" ItemsSource="{Binding AvailableMasters}" SelectedItem="{Binding SelectedMaster}">

When I run the application the SelectedMaster property is assigned the first item in AvailableMasters. Also, each time the AvailableMasters collection changes (for example, by assigning a new collection to the property) the SelectedMaster is again adjusted.

The desired behavior is that SelectedItem (SelectedMaster) is only populated/changed when the end-user clicks with the mouse on that item / chooses that item from the ComboBox or other control.


回答1:


Set a flag/bool property before you update the collection and use it in SelectedMaster property. Or do you need only XAML solution?



来源:https://stackoverflow.com/questions/45368609/wpf-controls-disable-changes-to-selecteditem-when-bound-property-to-itemssou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!