c#winforms绑定ListBox SelectedItem的属性 [英] c# winforms binding the properties of a ListBox SelectedItem

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

问题描述

所以我有一个列表框和一个类.该类称为任务",具有三个属性:Name,IsStarted和IsCompleted.

So I have a listbox and a class. This class is called 'Mission' and has three properties: Name, IsStarted and IsCompleted.

我使用了BindingList并将Listbox的数据源设置为该BindingList.效果很好,到目前为止没有问题.

I have used a BindingList and set the datasource of the Listbox to that BindingList. This works fine, no problems so far.

我不知道要怎么做(我希望有可能)是将属性IsStarted和IsCompleted绑定到两个CheckBox,这样,如果我更改选择的Mission,这些CheckBox就会改变以反映SelectedItem.

What I don't know how to do (and I hope it's possible) is to bind the properties IsStarted and IsCompleted to two CheckBoxes, so that if I change which Mission is selected, those CheckBoxes will change to reflect the properties of the SelectedItem.

我很高兴可以通过使用ListBox上的SelectedItemChanged事件来做到这一点,但我想进行两种方式的绑定,以便在我检查或取消选中CheckBoxes时,类中的数据将发生变化. /p>

I appreciate I can do this by using the event of SelectedItemChanged on the ListBox, but I would like to have a two way binding so that if I were to check or uncheck the CheckBoxes, the data in the class would change.

推荐答案

您不需要使用事件,使用数据绑定就足够了:

You don't need to use events, it's enough to use data binding:

missionsListBox.DataSource = bindingList;
missionsListBox.DisplayMember = "Name";
isStartedCheckBox.DataBindings.Add("Checked", bindingList, "IsStarted");
isCompletedCheckBox.DataBindings.Add("Checked", bindingList, "IsCompleted")

这样,ListBox将充当任务对象的索引,并显示任务列表.选择一个项目时,它会在相应的复选框控件中显示IsStartedIsCompleted的值,您可以使用复选框更改所选项目的那些值.

This way, the ListBox will act as an index for mission objects and it shows a list of missions. When you select an item, it shows values of IsStarted and IsCompleted in corresponding check box controls and you can change those values for selected item using check boxes.

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

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