Winforms:如何使用数据绑定绑定 CheckedListBox 的 Checkbox 项 [英] Winforms : How to bind the Checkbox item of a CheckedListBox with databinding

查看:19
本文介绍了Winforms:如何使用数据绑定绑定 CheckedListBox 的 Checkbox 项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据绑定的checkedlistbox,我想知道是否可以将每个列表框项目的复选框与对象的某个属性进行数据绑定.

I have a databinded checkedlistbox in one form and I would like to know if it is even possible to databind the check box of each list box item with a certain property of an object.

提前感谢您的帮助:)

也许我的问题被误解了.

Edit : Perhaps my question was misinterpreted.

我想知道是否可以为 CheckedListBox 的每个项目的复选框进行数据绑定.我知道如何将数据绑定到源以及如何通过迭代项目以编程方式更改条目.我不知道是否可以上课它实现了 INotifyPropertyChanged,因此当CheckedState"属性更改时,CheckedListBox 会自行更新.

I would like to know if it is possible to databind the checkbox for each Item of CheckedListBox. I know how to databind to a source and how to programatically change the entries by iterating through the itmes. What I don't know is if it is possible to have a class which implements INotifyPropertyChanged so that when a "CheckedState" property changes the CheckedListBox updates itself.

推荐答案

根据 Samich 的回答,这里是一个完整的例子,绑定源是一个 Object

According to Samich's answer, Here is a full example, the binding source is an Object

private void Form1_Load(object sender, EventArgs e)
        {
            List<randomClass> lst = new List<randomClass>();

            lst.Add(new randomClass());
            lst.Add(new randomClass());
            lst.Add(new randomClass());
            lst.Add(new randomClass());
            lst.Add(new randomClass());
            lst.Add(new randomClass());

            ((ListBox)this.checkedListBox1).DataSource = lst;
            ((ListBox)this.checkedListBox1).DisplayMember = "Name";
            ((ListBox)this.checkedListBox1).ValueMember = "IsChecked";


            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                randomClass obj = (randomClass)checkedListBox1.Items[i];
                checkedListBox1.SetItemChecked(i, obj.IsChecked);
            }
        }
    }

    public class randomClass
    {
        public bool IsChecked { get; set; }
        public string Name { get; set; }
        public randomClass()
        {
            this.IsChecked = true;
            Name = "name1";
        }
    }

randomClass 用于演示目的

这篇关于Winforms:如何使用数据绑定绑定 CheckedListBox 的 Checkbox 项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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