ComboBox SelectedItem与SelectedValue [英] ComboBox SelectedItem vs SelectedValue

查看:364
本文介绍了ComboBox SelectedItem与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.

Aside :为了我的目的,我结束了绑定至 SelectedItem SelectedValue 。这样我就可以根据UI变化(通过 SelectedValue 绑定)获取即时模型更新,并根据编程模型更改(通过 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 控件。

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


SelectedValue属性更改时发生。

Occurs when the SelectedValue property changes.

也就是说,他们不会触发 INotifyPropertyChanged PropertyChanged 事件是一样的,但他们将会反正。唯一的区别是射击事件。一旦从ComboBox的列表部分进行了新的选择, SelectedValueChanged 就被触发,并且 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与SelectedValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆