如何绑定列表计数中的WinForms标签吗? [英] How to bind a list count to a label in WinForms?

查看:134
本文介绍了如何绑定列表计数中的WinForms标签吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能绑定列表标签的数量。下面的代码开不得到与列表更新改变:

 私人的IList<串GT;名单=新名单,LT;串>(); 
// ...
label1.DataBindings.Add(文本,list.Count,);


解决方案

据马克Gravell对于这个问题,他有< A HREF =htt​​p://objectmix.com/csharp/388347-binding-formatstring-rendering-curly-braces-trick-bind-list-t-countproperty.html相对=nofollow>建议创建一个门面,它包装要绑定到label1.Text集合



我试图实现一个(的乐趣),并能得到结合的计数的工作

CountList< T> 是一个门面,它包装集合结合。



下面是完整的演示。

 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;使用System.Windows.Forms的
;

命名空间TextBindingTest
{
公共部分Form1类:表格
{
私人只读CountList<串GT; _List =
新CountList<串GT(新的List<串GT; {A,B,C});

公共Form1中()
{
的InitializeComponent();
BindAll();
}

私人无效BindAll()
{
无功约束力=新的绑定(文本,_List,计数,真正的);
binding.Format + =(发件人,E)=> e.Value =的String.Format({0}项目,e.Value);
label1.DataBindings.Add(结合);
}

私人无效addToList_Click(对象发件人,EventArgs五)
{
_List.Add(A);
}

私人无效closeButton_Click(对象发件人,EventArgs五)
{
关闭();
}
}

公共类CountList< T> :INotifyPropertyChanged的
{
公共事件PropertyChangedEventHandler的PropertyChanged =委托{};
私人无效OnPropertyChanged(PropertyChangedEventArgs E)
{
VAR处理器=的PropertyChanged;
处理器(这一点,E);
}

私人的ICollection< T>清单{搞定;组; }
公众诠释计数{{返回List.Count; }}

公共CountList(ICollection的< T>清单)
{
=列出清单;
}

公共无效添加(T项目)
{
List.Add(项目);
OnPropertyChanged(新PropertyChangedEventArgs(计数));
}
}
}


How can I bind the count of a list to a label. The following code does't get updated with the list is changed:

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

解决方案

According to Marc Gravell for this problem, he has suggested to create a facade that wraps the collection you want to bind to label1.Text

I have tried to implement one (for fun) and was able to get binding to Count working.
CountList<T> is a facade that wraps a collection to bind to.

Here is the full of the demo.

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标签吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆