结合使财产CheckedListBox.Items - 的WinForms [英] Binding enabled property from CheckedListBox.Items - Winforms

查看:201
本文介绍了结合使财产CheckedListBox.Items - 的WinForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是比较新的WinForms的编程,从ASP.NET来了,我挣扎绑定的东西,如从checklistbox.items已启用字段。

I'm relatively new to winforms programming, coming from ASP.NET and I'm struggling to bind things such as "Enabled" field from checklistbox.items.

例如,我有一个:

class A
{
    public string Name {get;set;}
    public bool Enabled {get; set;}
}

我然后创建一个列表项,并绑定到的CheckedListBox。

I then create a list items A and bind to a checkedlistbox.

List<A> aList = new List<A>(
    new A{Name="Item1", Enabled=true}, 
    new A{Name="Item2", Enabled=false} );

CheckedListBox.DataSource = aList;
CheckedListBox.DisplayMember = "Name";

最后,我如何与项目已启用== false以显示为残疾人士在的CheckedListBox?

Finally, how do I make items with "Enabled==false" to show as disabled in the checkedlistbox?

当然,我需要的Checked属性,但为了简便起见,我没有将它添加到的例子做同样的。

Of course, I'll need to do the same with the Checked property but for sake of simplicity I'm not adding it to the example.

谢谢

约翰·

推荐答案

您不能执行这种与的WinForms的CheckedListBox丰富的数据绑定。

You cannot perform this kind of rich databinding with the WinForms CheckedListBox.

您可以设置数据源,然后将DisplayMember和ValueMember属性,但是从那里对你要做的后半生与code(包括设置Enabled和经过性能)

You can set the DataSource and then the DisplayMember and ValueMember properties but from there on in you have to do the rest with code (including setting the Enabled and Checked properties)

设置基本的约束力,如下所示:

Set the basic binding as shown below:

checkedListBox.DataSource = aList; 
checkedListBox.DisplayMember = "Name";
checkedListBox.ValueMember = "Name";

在那里,您将需要需要遍历数据源,设置单独的项目属性。

From there you will need to iterate over the datasource, setting the individual item properties as needed.

,你可能能够做一件事(我还没有试过,但它应该工作)是子类的CheckedListBox和,但一些自定义绑定code。在新的类。

One thing that you might be able to do (I haven't tried this but it should work) is subclass the CheckedListBox and but some custom binding code in the new class.

这可能会给你一个更好的解决方案 - 从$ C $Ç背后取下code,但根据您的情况可能不值得的努力

That might give you a more elegant solution - removing code from the code behind, though depending on your situation might not be worth the effort.

这篇关于结合使财产CheckedListBox.Items - 的WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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