绑定列表到数据源

编程入门 行业动态 更新时间:2024-10-25 18:23:46
本文介绍了绑定列表到数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望能够到一个列表绑定到一个列表框的数据源,并在列表被修改,列表框的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

更多推荐

绑定列表到数据源

本文发布于:2023-08-05 21:36:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1308196.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据源   绑定   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!