DataBind listBox到列表 [英] DataBind listBox to List

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

问题描述

我有一个已绑定到List< t>集合对象,但当我在List< t>中进行更改时对象,它没有反映在我的列表"框中.我在这里做错了什么?

哦,顺便说一句,我正在使用Windows窗体.这是我的代码:

I have a listbox that I''ve binded to a List<t> collection object but when I make changes in the List<t> object it''s not being reflected in the my listbox. What am I doing wrong here?

Oh, by the way I''m using windows form. Here''s my code:

class MyDataObject
{
   List<string> data;

   public MyDataObject()
   {
      data = new List<string>() {"abc","rst","xyz"};
   }

   public List<string> Data
   {
     get{return data;}
     set{data = value;}
   }
}


// Windows Form section

MyDataObject datObj = MyDataObject();

public Form()
{
   Initialization();
  
   listbox1.DataSource = datObj.Data;
   listbox1.DisplayMember = "Data";
}

void button1_Click(object sender, EventArgs e)
{
  string newData = textbox1.Text;
  datObj.Data.Add(newData);

  listbox1.Refresh();
}
</string></string></string>


列表框最初显示了dataObj的Data项,但是当我将新数据添加到文本框并将其传递给我的dataObject并单击button1时,它没有显示在我认为已绑定到datObj的Data的列表框中


The listbox initially shows the dataObj''s Data items but when I add new data to the textbox and pass it to my dataObject and click button1 it''s not displaying in my listbox which I thought was binded to my datObj''s Data memeber?

推荐答案

而不是List< t> ;,而是使用ObservableCollection< t>.要获得有关插入的通知,列表本身不会通知更改.
Instead of a List<t>, use an ObservableCollection<t> to get notified of insertions, the List itself does not notify changes.


您好,

尝试使用BindingList< t>代替List或List t.该列表不会触发更改后的更新,因此您的列表框也不会被更新.

关于AT
Hi,

Try using BindingList<t> in stead or List<t>. the list does not fire an changed update and as such your list box will not be updated.

Regards, AT


您需要重新绑定列表视图以刷新其值.

问候,
爱德华
You need to rebind the listview to refresh its values.

Regards,
Eduard


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

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