CheckedListBox数据绑定到Items的选中状态 [英] CheckedListBox data binding to checked state of Items

查看:732
本文介绍了CheckedListBox数据绑定到Items的选中状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图这样设置CheckedListBox的数据源:

I tried to set DataSource of CheckedListBox like this:

private void Form1_Load(object sender, EventArgs e)
    {
        checkedListBox1.DisplayMember = "Name";
        checkedListBox1.ValueMember = "Checked";
        _bindingList = new BindingList<CustomBindingClass>(
            new List<CustomBindingClass>
        {
            new CustomBindingClass {Checked = CheckState.Checked, Name = "Item1"},
            new CustomBindingClass {Checked = CheckState.Checked, Name = "Item2"},
            new CustomBindingClass {Checked = CheckState.Unchecked, Name = "Item3"},
        });
        checkedListBox1.DataSource = _bindingList;
    }

而且它可以正常运行,但部分运行。我以后可以休假

And It's working but partially. I'm able to do the fallowing later

_bindingList.RemoveAt(0);

_bindingList [0] .Name = TestTest; 和CheckedListBox更新得很好,除了未检查项目。这不起作用

or _bindingList[0].Name = "TestTest"; and CheckedListBox updates well except items are not checked. This is not working

_bindingList[0].Checked=CheckState.Checked;

我还测试了在选中时执行的操作我的 CustomBindingClass 中的属性是bool类型,但也不起作用。任何建议 ValueMember 属性的类型是什么?

I also tested to do it when CheckedProperty from my CustomBindingClass is of type bool, but doesn't works either. Any suggestion what should be the type of ValueMember property ?

推荐答案

考虑这些事实:


  1. CheckedListBox 没有内置功能数据绑定支持检查项目。您需要自己处理项目的检查状态。

  1. CheckedListBox does't have a built-in data-binding support for checking items. You need to handle check state of items yourself.

您设置 checkedListBox1.ValueMember = Checked; 。您没有设置项目检查状态,只是说选择项目时,由 SelectedValue 返回的值来自 Checked 对象的属性,该属性位于被选中的项目后面。例如,您可以在 Button Click 事件中使用此代码以查看结果;无论项目的检查状态如何,该消息框都会显示该项目后面对象的已检查属性值:

You set checkedListBox1.ValueMember = "Checked";. You didn't set item check state, you just said when you select the item, the value which returns by SelectedValue comes from Checked property of your object which is behind the seected item. For example you can use this code in a Click event of a Button to see the result; regardless of check-state of items, the message box, will show value of Checked property of the object behind the item:

MessageBox.Show(checkedListBox1.SelectedValue.ToString());


  • 选择和检查项目完全不同。

  • Selecting and checking items are completely different.

    我更喜欢将 DataGridView 用于此目的。您只需拥有一个 CheckBox 列和一个只读的 TextBox 列并绑定 DataGridView 到对象列表。

    I prefer to use DataGridView for such purpose. You can simply have a CheckBox column and a readonly TextBox column and bind DataGridView to the list of your objects.

    如果需要双向数据绑定,则需要实现 INotifyPropertyChanged 界面,无论您要显示什么控件数据。如果未实现该接口,则在更改模型的属性时,不会发生 ListChange 事件,并且不会自动看到UI更改。

    If you need to have two-way data binding, you need to implement INotifyPropertyChanged interface regardless of what control you are using to show data. If you don't implement that interface, when changing properties on your model ListChange event will not raise and you can not see changes in UI automatically.

    这篇关于CheckedListBox数据绑定到Items的选中状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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