【问题标题】:How To Bind to CheckedListBox.SelectedItems.Count如何绑定到 CheckedListBox.SelectedItems.Count
【发布时间】:2017-02-07 23:14:03
【问题描述】:

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

无法绑定到 DataSource 上的属性或列 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# 版本 - 我可以而且很乐意转换它。

【问题讨论】:

    标签: c# vb.net winforms data-binding


    【解决方案1】:

    由于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();
        }));
    };
    

    【讨论】:

    • ItemCheck 事件不显示 CheckedItems.Count - 直到事件之后才更新该值。我知道我可以得到 e.newValue - 我不想要那个,我想要实际的计数 - 而不是 1 折扣。这就是为什么我想走绑定路线。在我的搜索中,这似乎是不可能的。
    • 直到 ItemCheck 事件发生后才会更新检查状态。。请参阅编辑,另见this post。关键是使用BeginInvoke 来询问项目数量。这样,检查/取消检查后的项目计数将返回给您。
    • 是的,我正在阅读这篇文章:stackoverflow.com/questions/3181985/…我现在正在测试它。
    • 使用上面的代码即可。你不需要处理SelectedIndexChanged。使用ItemCheck 就足够了!你只需要使用BeginInvoke
    猜你喜欢
    • 1970-01-01
    • 2012-03-30
    • 2015-03-10
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多