ComboBox SelectedItem vs SelectedValue [英] ComboBox SelectedItem vs SelectedValue
问题描述
下面的代码正如你所期望的那样工作 - 当用户在下拉菜单中选择一个新项目时,模型上的 MyProperty
会更新。
The following code works as you’d expect — MyProperty
on the model is updated when the user picks a new item in the dropdown.
comboBox1.DataBindings.Add("SelectedValue", myModel, "MyProperty", true,
DataSourceUpdateMode.OnPropertyChanged);
但是,以下操作不能以同样的方式工作,输入焦点移动到表单上的另一个控件:
The following, however, doesn’t work the same way and the model update isn’t triggered until the input focus moves to another control on the form:
comboBox1.DataBindings.Add("SelectedItem", myModel, "MyProperty", true,
DataSourceUpdateMode.OnPropertyChanged);
有人知道为什么吗?我甚至不知道从哪里开始调查的原因。
Does anybody know why? I don’t even know where to start investigating the cause. Pointers in the right direction to start the investigation or an outright explanation would be equally appreciated.
:为了我的目的,我结束了绑定到 SelectedItem
和 SelectedValue
。这样,我基于UI更改(通过 SelectedValue
绑定)获得即时模型更新,以及基于编程模型更改的UI更新(通过 SelectedItem
binding)。
Aside: for my purposes, I ended up binding to both SelectedItem
and SelectedValue
. This way I get instant model updates based on UI changes (through the SelectedValue
binding), and UI updates based on programmatic model changes (through the SelectedItem
binding).
推荐答案
ComboBox
控制继承了 ListControl
控制。
The ComboBox
control inherits from the ListControl
control.
SelectedItem
属性是 ComboBox
控件的正确成员。更改时触发的事件是 ComboBox.SelectionChangeCommitted
The SelectedItem
property is a proper member of the ComboBox
control. The event that is fired on change is ComboBox.SelectionChangeCommitted
ComboBox.SelectionChangeCommitted
当所选项目已更改并且更改显示在ComboBox中时发生。
Occurs when the selected item has changed and that change is displayed in the ComboBox.
SelectedValue
属性从 ListControl
控件继承。
因此,此属性将触发 ListControl.SelectedValueChanged
事件。
The SelectedValue
property is inherited from the ListControl
control.
As such, this property will fire the ListControl.SelectedValueChanged
event.
ListControl.SelectedValueChanged
ListControl.SelectedValueChanged
当SelectedValue属性更改时发生。
Occurs when the SelectedValue property changes.
也就是说,他们不会触发 INotifyPropertyChanged
。 PropertyChanged
事件相同,但他们会反正。唯一的区别是在发射事件。 SelectedValueChanged
在从ComboBox的列表部分进行新选择时触发, SelectedItemChanged
该项目显示在ComboBox的TextBox部分。
That said, they won't fire the INotifyPropertyChanged
.PropertyChanged
event the same, but they will anyway. The only difference is in the firing event. SelectedValueChanged
is fired as soon as a new selection is made from the list part of the ComboBox, and SelectedItemChanged
is fired when the item is displayed in the TextBox portion of the ComboBox.
简而言之,它们都代表ComboBox的列表部分。因此,当绑定任一属性时,结果是相同的,因为 PropertyChanged
事件在任一情况下触发。
In short, they both represent something in the list part of the ComboBox. So, when binding either property, the result is the same, since the PropertyChanged
event is fired in either case. And since they both represent an element from the list, the they are probably treated the same.
这是否有帮助?
编辑#1
假设ComboBox的列表部分代表一个属性(因为我不能确认, t写控件),绑定 SelectedItem
或 SelectedValue
会影响控件中的相同集合。然后,当该属性改变时,最终发生同样的情况。 INotifyPropertryPropertyChanged.PropertyChanged
事件在同一个属性上触发。
Assuming that the list part of the ComboBox represents a property (as I can't confirm since I didn't write the control), binding either of SelectedItem
or SelectedValue
affects the same collection inside the control. Then, when this property is changed, the same occurs in the end. The INotifyPropertryPropertyChanged.PropertyChanged
event is fired on the same property.
这篇关于ComboBox SelectedItem vs SelectedValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!