组合框 SelectedItem 与 SelectedValue [英] ComboBox SelectedItem vs SelectedValue

查看:38
本文介绍了组合框 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.

旁白:出于我的目的,我最终绑定到 SelectedItemSelectedValue.通过这种方式,我可以获得基于 UI 更改(通过 SelectedValue 绑定)的即时模型更新,以及基于编程模型更改(通过 SelectedItem 绑定)的 UI 更新.

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 事件相同,但无论如何它们都会发生.唯一的区别在于触发事件.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 的列表部分代表一个属性(因为我没有编写控件,所以我无法确认),绑定 SelectedItemSelectedValue 影响控件内的相同集合.然后,当更改此属性时,最终会发生同样的情况.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.

这篇关于组合框 SelectedItem 与 SelectedValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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