将WinForms ListBox绑定到对象属性 [英] Binding WinForms ListBox to object properties

查看:170
本文介绍了将WinForms ListBox绑定到对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次做一些WinForms编码,并尝试使用数据绑定。



我有一个列表框,我绑定到一个字符串数组,从我的控制器对象,我也想将SelectedItem从列表框绑定到控制器上的另一个字符串属性,以便我可以跟踪它。

  listBox.DataSource = controller.ItemNames; 
listBox.DataBindings.Add(new Binding(SelectedItem,controller,CurrentItem));

我希望控制器上的CurrentItem属性在用户在列表框,但似乎只有当焦点移动到表单上的另一个项目时才会更新。



这是预期的行为吗?有没有办法让SelectedItem绑定更新?

解决方案

使用 DataSourceUpdateMode.OnPropertyChanged

  listBox.DataBindings.Add(new Binding(SelectedItem,controller,CurrentItem,
true,DataSourceUpdateMode.OnPropertyChanged));


I am doing some WinForms coding for the first time and am trying to use data bindings.

I have a listbox which I bind to an array of strings from my controller object and I also want to bind the SelectedItem from the list box to another string property on the controller so I can track it.

listBox.DataSource = controller.ItemNames;
listBox.DataBindings.Add(new Binding("SelectedItem", controller, "CurrentItem"));

I want the CurrentItem property on the controller to be updated as soon as the user selects different items in the listbox, but it seems that it will only get updated when focus moves to another item on the form.

Is this the expected behavior? Is there a way to have the SelectedItem binding update immediately?

解决方案

Use DataSourceUpdateMode.OnPropertyChanged

listBox.DataBindings.Add(new Binding("SelectedItem", controller, "CurrentItem", 
                                true, DataSourceUpdateMode.OnPropertyChanged));

这篇关于将WinForms ListBox绑定到对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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