【问题标题】:How to bind a list count to a label in WinForms?如何将列表计数绑定到 WinForms 中的标签?
【发布时间】:2009-03-12 18:27:52
【问题描述】:

如何将列表的计数绑定到标签。以下代码不会随着列表的更改而更新:

private IList<string> list = new List<string>();
//...
label1.DataBindings.Add("Text", list.Count, "");

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    根据 Marc Gravell 对于这个问题的说法,他有 suggested to create a facade 包装了您要绑定到 label1.Text 的集合

    我已经尝试实现一个(为了好玩)并且能够绑定到 Count 工作。
    CountList&lt;T&gt; 是包装要绑定到的集合的外观。

    这是完整的演示。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Windows.Forms;
    
    namespace TextBindingTest
    {
        public partial class Form1 : Form
        {
            private readonly CountList<string> _List =
                new CountList<string>(new List<string> { "a", "b", "c" });
    
            public Form1()
            {
                InitializeComponent();
                BindAll();
            }
    
            private void BindAll()
            {
                var binding = new Binding("Text", _List, "Count", true);
                binding.Format += (sender, e) => e.Value = string.Format("{0} items", e.Value);
                label1.DataBindings.Add(binding);
            }
    
            private void addToList_Click(object sender, EventArgs e)
            {
                _List.Add("a");
            }
    
            private void closeButton_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    
        public class CountList<T> : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged = delegate { };
            private void OnPropertyChanged(PropertyChangedEventArgs e)
            {
                var handler = PropertyChanged;
                handler(this, e);
            }
    
            private ICollection<T> List { get; set; }
            public int Count { get { return List.Count; } }
    
            public CountList(ICollection<T> list)
            {
                List = list;
            }
    
            public void Add(T item)
            {
                List.Add(item);
                OnPropertyChanged(new PropertyChangedEventArgs("Count"));
            }
        }
    }
    

    【讨论】:

    • 我认为这可以完成工作,但是男孩是不是很复杂!你会认为应该有更简单的方法来做这样的简单绑定。在我看来,Winforms 中的所有数据绑定功能都是为了迎合列表和网格。
    • 是的,它看起来确实比它需要的复杂。我最近开始研究 WPF,这种烦人的绑定问题似乎在 XAML 中得到了解决
    【解决方案2】:

    绑定监听 IPropertyChanged 接口的 PropertyChanged 事件。我认为 List.Count 在更改时不会报告为 PropertyChanged 事件。

    您可以做的是实现您的自定义 List 或找到一个在 Count 更改时通知的集合。

    【讨论】:

    • 我认为 BindingList 类会在它发生变化时触发事件,这样可能会更好。
    【解决方案3】:

    如果您有列表框的数据源,则可以使用 DataSourceChanged。请记住更新并重新绑定数据源。

    这可能有点贫民窟,但这是我使用的示例:

    List<int> collection = new List<int>();
        public Form1()
        {
            InitializeComponent();
            listBox1.DataSourceChanged += listbox1_Changed;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            collection.Add(new Random().Next(100));
            listBox1.BeginUpdate();
            listBox1.DataSource = null;
            listBox1.DataSource = collection;
            listBox1.EndUpdate();
        }
        private void listbox1_Changed(object sender, EventArgs e)
        {
            textBox1.Text = collection.Count.ToString();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      • 2012-02-12
      • 2011-02-17
      • 2010-09-20
      • 2012-10-12
      相关资源
      最近更新 更多