c# - 如何绑定(bind)到 CheckedListBox.SelectedItems.Count

标签 c# vb.net winforms data-binding

我正在尝试将标签绑定(bind)到 CheckedListBox.CheckedItems.Count 我已经尝试了几种方法并收到消息:

Cannot bind to the property or column Count on the DataSource. Parameter name: 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# 版本 - 我可以而且很乐意转换它。

最佳答案

由于 CheckListBox 不支持多选,您可能指的是 CheckItems.Count。您不能绑定(bind)到 CheckItems.Count。要收到有关 CheckedItem.Count 更改的通知,您应该处理 ItemCheck CheckedListBox 的事件:

C#

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

关于c# - 如何绑定(bind)到 CheckedListBox.SelectedItems.Count,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39773497/

相关文章:

c# - 在 C# 中确定对象 o 是 Action<...> 还是 Func<....>

c# - 如何以编程方式确定从主页功能区创建的 Outlook 新项目 --> 新项目

c# - UWP:替换数据绑定(bind)

c# - 使 WPF TextBlock 只增长而不收缩?

正则表达式:如果字符串中的特定单词匹配,如何使 IsMatch 失败

mysql - 如何从 mysql 填充 datagridview 组合框 - vb.net

c# - WCF REST 服务中返回大文件的响应时间问题

c# - WebService 中的 View 状态?这可能吗?

c# - 在c#中将文件写入用户桌面

c# - 在图片框控件中显示后处理位图