组合框的SelectedItem VS的SelectedValue [英] ComboBox SelectedItem vs SelectedValue

查看:310
本文介绍了组合框的SelectedItem VS的SelectedValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code可以作为你所期望的 - 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);

是否有人知道为什么吗?我甚至不知道从哪里开始调查原因。在正确的方向指针开始调查或彻底解释是同样AP preciated。

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 。这样,我得到一个基于用户界面的变化即时更新模型(通过的SelectedValue 绑定),并基于编程模型更改UI更新(通过的SelectedItem 绑定)。

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).

推荐答案

的<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx\"><$c$c>ComboBox控制从<一个继承href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.aspx\"><$c$c>ListControl控制。

的<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selecteditem.aspx\"><$c$c>SelectedItem属性是组合框控制适当的成员。那是在变化的触发事件是<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectionchangecommitted.aspx\"><$c$c>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

在选定的项目更改后发生和变化显示在组合框。

Occurs when the selected item has changed and that change is displayed in the ComboBox.

的<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.selectedvalue.aspx\"><$c$c>SelectedValue属性是从列表控件控制继承。
因此,此属性将触发<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.selectedvaluechanged.aspx\"><$c$c>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.

这表示,他们将不火的<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx\"><$c$c>INotifyPropertyChanged.<$c$c>PropertyChanged事件是相同的,但他们会反正。唯一不同的是在烧制事件。 SelectedValueChanged 是只要一个新的选择从组合框列表中一部分并发射 SelectedItemChanged 是发射时该项目将显示在组合框的文本框部分。

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.

总之,他们都在重新组合框的列表部分present东西。因此,结合任一属性时,结果是一样的,因为的PropertyChanged 事件在这两种情况下烧制。而且,由于他们都重新present从列表中的元素,他们可能同样对待。

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

假设组合框重新$ P $列表部分psents一个属性(如我无法证实,因为我没有写控制),结合C>或的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.

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

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