CheckedListBox checked list item property binding to field in a class

后端 未结 2 1116
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-18 17:13

I have a class with two properties and I want to bind it to a CheckedListBox item. WebsiteName property will be the DisplayValue and IsChecked should be the Checked state of

相关标签:
2条回答
  • 2021-01-18 17:34

    The CheckedListBox control doesn't really support a DataSource, which is why the designers hid it from Intellisense. You can use it, but the checkmark property of the listbox won't work with your class, even if you set the ValueMember property to your boolean property.

    Unfortunately, it means you have to set the values yourself:

    checkedlistbox.DataSource = list;
    checkedlistbox.DisplayMember = "WebsiteName";
    for (int i = 0; i < checkedListbox.Items.Count; ++i) {
      checkedListbox.SetItemChecked(i, ((ACLASS)checkedListbox.Items[i]).IsChecked);
    }
    

    and you also have to track the changes yourself, too:

    checkedListBox1.ItemCheck += (sender, e) => {
      list[e.Index].IsChecked = (e.NewValue != CheckState.Unchecked);
    };
    
    0 讨论(0)
  • 2021-01-18 17:49
    checkedlistbox.ValueMember = "IsChecked";  
    
    0 讨论(0)
提交回复
热议问题