简体   繁体   English

如何绑定到 CheckedListBox.SelectedItems.Count

[英]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:我正在尝试将标签绑定到 CheckedListBox.CheckedItems.Count 我已经尝试了几种方法并收到消息:

Cannot bind to the property or column Count on the DataSource.无法绑定到 DataSource 上的属性或列 Count。 Parameter name: dataMember参数名称: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}"


    lblBGCount.DataBindings.Add(BgCountBinding)

I know the code is VB but if you have a C# version - I can and will be happy to convert it.我知道代码是 VB,但如果您有 C# 版本 - 我可以并且很乐意转换它。

Since the CheckListBox doesn't support multi-selection, probably you mean CheckItems.Count .由于CheckListBox不支持多选,因此您的意思可能是CheckItems.Count You can not bind to CheckItems.Count .您不能绑定到CheckItems.Count To be notified about changing in CheckedItem.Count you should handle ItemCheck event of the CheckedListBox :要获得有关CheckedItem.Count更改的通知,您应该处理CheckedListBox ItemCheck事件:

C# C#

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM