WPF控件,当对ItemsSource的绑定属性发生更改时,禁用对SelectedItem的更改 [英] WPF controls, disable changes to `SelectedItem` when bound property to `ItemsSource` changes
问题描述
当我使用 ComboBox
或其他具有 ItemsSource
和 SelectedItem
属性绑定,然后每次在运行时进行初始绑定时,以及每次 ItemsSource
的绑定集合更改时,我都会体验到绑定的<$ c $的内容c> SelectedItem 对象已更改。
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.
如何禁用此功能?
例如:
我有< ComboBox MinWidth = 300 ItemsSource = {Binding AvailableMasters} SelectedItem = {Binding SelectedMaster}>
当我运行应用程序时, SelectedMaster
属性被分配给 AvailableMasters
中的第一项。此外,每次 AvailableMasters
集合发生更改(例如,通过向属性分配新集合), 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.
所需的行为是 SelectedItem
( SelectedMaster
)仅在最终用户用鼠标单击该项目/从 ComboBox
或其他控件中选择该项目时填充/更改。
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.
推荐答案
在更新集合并在SelectedMaster属性中使用它之前,请设置标志/布尔属性。还是只需要XAML解决方案?
Set a flag/bool property before you update the collection and use it in SelectedMaster property. Or do you need only XAML solution?
这篇关于WPF控件,当对ItemsSource的绑定属性发生更改时,禁用对SelectedItem的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!