Winforms ComboBox 2种方式进行数据绑定仅1种方式 [英] Winforms ComboBox 2 way databinding only works 1 way

查看:69
本文介绍了Winforms ComboBox 2种方式进行数据绑定仅1种方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组合框,在设计视图中,它具有如下绑定到绑定源的数据:

I have a combobox, and in the design view I have it databound to a bindingsource as follows:

this.itemTypeComboBox.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.itemTypeContainerBindingSource, "ItemType", true));
this.itemTypeComboBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.itemTypeContainerBindingSource, "ItemType", true));

在load事件背后的代码中,我有以下内容:

In the code behind in the load event I have the following:

// bind the combobox to the enum
this.itemTypeComboBox.DataSource = Enum.GetValues(typeof(OpticalItemType));

// bind a custom object to the datasource
this.itemTypeContainerBindingSource.DataSource = customObjectContainer;

customObjectContainer是一个单个对象,其中包含绑定到组合框的属性 ItemType ,并且该对象的所有属性都通过 INotifyPropertyChanged使用更改通知。

The "customObjectContainer" is a single object that contains a property "ItemType" that is bound to the combobox, and all properties of the object use change notification through "INotifyPropertyChanged".

在我后面的代码中,如果我以编程方式更改自定义对象,则更改将反映在组合框中。但是,如果我通过UI更改组合框,则绑定源以及自定义对象均无法反映更改。

In my code behind, if I programmatically change the custom object, the changes are reflected in the combobox. However, if I change the combobox through the UI, the bindingsource, and hence the custom object do not reflect the changes.

有什么想法我做错了吗?

Any ideas what I am doing wrong?

由于它是单个对象,因此与使用BindingList等无关。

As it is a singular object, it cannot be to do with using BindingList etc.

好吧,每当我通过UI更改组合框时,它都不会更改基础对象,也不会为自定义对象中的属性命中设置方法。但是,我刚刚注意到,如果我退出该控件,它将触发设置器,并更改基础对象。为什么?

Ok, whenever I change the combobox through the UI, it never changes the underlying object, the setter is never hit for the property in the custom object. However, I have just noticed that if I tab off of the control, it then fires the setter, and changes the underlying object. Why would this be?

看来,问题出在我的约束力上。我在绑定中添加了 DataSourceUpdateMode.OnPropertyChanged,现在可以正常使用!

It appears, the issue was with my binding. I added "DataSourceUpdateMode.OnPropertyChanged" to the bindings, and it works now!!

推荐答案

问题出在我的绑定上。我在绑定中添加了 DataSourceUpdateMode.OnPropertyChanged,现在可以正常使用!

The issue was with my binding. I added "DataSourceUpdateMode.OnPropertyChanged" to the bindings, and it works now!!

这篇关于Winforms ComboBox 2种方式进行数据绑定仅1种方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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