如何绑定checkedlistbox . selecteitems . count

本文关键字:checkedlistbox selecteitems count 绑定 何绑定 | 更新日期: 2023-09-27 18:09:34

我试图将标签绑定到checkedlistbox . checkedititems . count我尝试了几种方法来解决这个问题,并收到了消息:

不能绑定到数据源上的属性或列计数。参数名称:dataMember

我的代码如下:
    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}"

    lblBGCount.DataBindings.Add(BgCountBinding)

我知道代码是VB,但如果你有一个c#版本-我可以并将很乐意转换它。

如何绑定checkedlistbox . selecteitems . count

由于CheckListBox不支持多选择,可能您指的是CheckItems.Count。不能绑定到CheckItems.Count。要获得关于CheckedItem.Count更改的通知,您应该处理CheckedListBoxItemCheck事件:

c#

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