Ken Ken - 2 months ago 6 Question

How To Bind to CheckedListBox.SelectedItems.Count

I am trying to bind a label to a CheckedListBox.CheckedItems.Count
I have tried a couple approaches to this and receive the message:

Cannot bind to the property or column Count on the DataSource.
Parameter name: dataMember

My Code is as Follows:

Dim BgCountBinding As Binding = New Binding("Text", BgCheckedListBox.CheckedItems, "Count")

' I have also tried this:
' Dim BgCountBinding As Binding = New Binding("Text", BgCheckedListBox, "CheckedItems.Count")

BgCountBinding.DataSourceUpdateMode = DataSourceUpdateMode.Never
BgCountBinding.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged
BgCountBinding.NullValue = "0"
BgCountBinding.FormattingEnabled = True
BgCountBinding.FormatString = "#: {0}"


I know the code is VB but if you have a C# version - I can and will be happy to convert it.


Since the CheckListBox doesn't support multi-selection, probably you mean CheckItems.Count. You can not bind to CheckItems.Count. To be notified about changing in CheckedItem.Count you should handle ItemCheck event of the CheckedListBox:


this.checkedListBox1.ItemCheck += (s, ea) =>
    this.BeginInvoke(new Action(() =>
        this.label1.Text = this.checkedListBox1.CheckedItems.Count.ToString();