的WinForms:如何将的CheckedListBox的复选框项绑定数据绑定 [英] Winforms : How to bind the Checkbox item of a CheckedListBox with databinding

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

问题描述

我有一个databinded的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.

感谢任何帮助提前:)

编辑:也许我的问题是misinter preTED。

Edit : Perhaps my question was misinterpreted.

我想知道是否有可能进行数据绑定的复选框的CheckedListBox的每个项目。我知道如何进行数据绑定到源,以及如何通过该itmes迭代编程的方法修改的条目。我不知道的是,如果有可能有一个类
它实现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的回答,下面是一个完整的例子,绑定源是对象

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的复选框项绑定数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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