问题
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);
or _bindingList[0].Name = "TestTest";
and CheckedListBox updates well except items are not checked. This is not working
_bindingList[0].Checked=CheckState.Checked;
I also tested to do it when Checked
Property from my CustomBindingClass
is of type bool, but doesn't works either. Any suggestion what should be the type of ValueMember
property ?
回答1:
Consider these facts:
CheckedListBox
does't have a built-in data-binding support for checking items. You need to handle check state of items yourself.You set
checkedListBox1.ValueMember = "Checked";
. You didn't set item check state, you just said when you select the item, the value which returns bySelectedValue
comes fromChecked
property of your object which is behind the seected item. For example you can use this code in aClick
event of aButton
to see the result; regardless of check-state of items, the message box, will show value ofChecked
property of the object behind the item:MessageBox.Show(checkedListBox1.SelectedValue.ToString());
Selecting and checking items are completely different.
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.
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.
回答2:
If you take a look at CheckedListBox
class, you'll notice that DataSource
, DisplayMember
and ValueMember
are marked with
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
This a common technique used in Windows Forms controls to indicate that some public properties inherited from a base class (hence cannot be removed) are not applicable for that concrete derived class and should not be used.
There must be a reason for doing that for the aforementioned properties of the CheckedListBox
. As you already saw, it's "sort of working", but the point is that it isn't guaranteed to work at all. So don't use them. If you wish, create a helper class that holds CheckedListBox
and BindingList
, listens to ListChanged
event and synchronizes the control.
来源:https://stackoverflow.com/questions/39075755/checkedlistbox-data-binding-to-checked-state-of-items