绑定列表到数据源 [英] Bind List to DataSource

查看:132
本文介绍了绑定列表到数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够到一个列表绑定到一个列表框的数据源,并在列表被修改,列表框的UI自动更新。 (没有的WinForms ASP)。
下面是一个例子:

 私人列表<富> fooList =新的List<富>(); 

私人无效Form1_Load的(对象发件人,EventArgs五)
{
//添加第一美孚在fooList
美孚foo1 =新的Foo(BAR1);
fooList.Add(foo1);

//绑定fooList到列表框
listBox1.DataSource = fooList;
//我可以看到在BAR1预期的列表框
}

私人无效的button1_Click(对象发件人,EventArgs五)
{
//添加anthoter美孚在fooList
富foo2的=新的Foo(BAR2);
fooList.Add(foo2的);
//我期望列表框UI进行更新感谢INotifyPropertyChanged的,但它不是
}

类Foo:INotifyPropertyChanged的
{
私人字符串bar_ ;
公共串吧
{
{返回bar_; }

{
bar_ =价值;
NotifyPropertyChanged(酒吧);
}
}

公共美孚(串吧)
{
this.Bar =栏;
}

公共事件PropertyChangedEventHandler的PropertyChanged;

私人无效NotifyPropertyChanged(字符串信息)
{
如果(的PropertyChanged!= NULL)
{
的PropertyChanged(这一点,新PropertyChangedEventArgs(信息)) ;
}
}

公共重写字符串的ToString()
{
返回bar_;
}
}

如果我更换列表<美孚> fooList =新的List<富>(); 的BindingList<富> fooList =新的BindingList<富>(); 然后它工作。但我不想改变原有的类型foolist的。我想是这样的工作: listBox1.DataSource =新的BindingList<富>(fooList);



编辑:另外我刚刚读到这里名单< T> VS的BindingList< T>优点/缺点从伊利亚Jerebtsov 的:当你设置的BindingSource的DataSource到List<>,它在内部创建一个的BindingList来包装你的名单。我觉得我的样本只是表明,这是不正确的:我的名单,LT;>似乎并没有在内部被裹成的BindingList<>


解决方案

您没有 BindingSource的你例子。



您需要修改它像这样使用的BindingSource

 变种BS =新的BindingSource(); 
美孚foo1 =新的Foo(BAR1);
fooList.Add(foo1);

bs.DataSource = fooList; //< - 对interrest

//绑定fooList到列表框
listBox1.DataSource = BS点; //< - 笔记它需要整个的BindingSource



修改



注意(如被指出在评论) - BindingSource的不 INotifyPropertyChanged的

I want to be able to bind a list to a listbox datasource and when the list is modified, the listbox's UI automatically updated. (Winforms not ASP). Here is a sample :

private List<Foo> fooList = new List<Foo>();

    private void Form1_Load(object sender, EventArgs e)
    {
        //Add first Foo in fooList
        Foo foo1 = new Foo("bar1");
        fooList.Add(foo1);

        //Bind fooList to the listBox
        listBox1.DataSource = fooList;
        //I can see bar1 in the listbox as expected
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //Add anthoter Foo in fooList
        Foo foo2 = new Foo("bar2");
        fooList.Add(foo2);
        //I expect the listBox UI to be updated thanks to INotifyPropertyChanged, but it's not
    }

class Foo : INotifyPropertyChanged
{
    private string bar_ ;
    public string Bar
    {
        get { return bar_; }
        set 
        { 
            bar_ = value;
            NotifyPropertyChanged("Bar");
        }
    }

    public Foo(string bar)
    {
        this.Bar = bar;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    public override string ToString()
    {
        return bar_;
    }
}

If I replace List<Foo> fooList = new List<Foo>(); by BindingList<Foo> fooList = new BindingList<Foo>(); then it works. But I don't want to change the original type of foolist. I would like something like this to work : listBox1.DataSource = new BindingList<Foo>(fooList);

EDIT : Also I just read here List<T> vs BindingList<T> Advantages/DisAdvantages from Ilia Jerebtsov : "When you set a BindingSource's DataSource to a List<>, it internally creates a BindingList to wrap your list". I think my sample just demonstrates that this is not true : my List<> doesn't seem to be internally wrapped into a BindingList<>.

解决方案

You don't have a BindingSource in you example.

you need to modify it like this to use a BindingSource

   var bs = new BindingSource();
   Foo foo1 = new Foo("bar1");
   fooList.Add(foo1);

     bs.DataSource = fooList; //<-- point of interrest

    //Bind fooList to the listBox
    listBox1.DataSource = bs; //<-- notes it takes the entire bindingSource

Edit

Be aware that (as was pointed out in comments) - the bindingsource does not work with INotifyPropertyChanged

这篇关于绑定列表到数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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