[英]Bind observable collection to listbox in asp.net c#
Heres my code 这是我的代码
ObservableCollection<product> pr = new ObservableCollection<product>();
protected void Page_Load(object sender, EventArgs e)
{
product p = new product();
p.PhoneMake = "Apple";
p.PhoneModel = "4S";
p.Price = 40;
pr.Add(p);
product q = new product();
p.PhoneMake = "Apple";
p.PhoneModel = "5S";
p.Price = 80;
pr.Add(q);
lstbxProd.DataSource = pr;
its not updating the listbox. 它不会更新列表框。 Do I need items source or what can i do?
我需要物品来源还是该怎么办?
I tried this too 我也尝试过
foreach (product a in pr)
{
lstbxProd.Items.Add(a).ToString;
}
You will need to subscribe to the CollectionChanged event of the Observable collection. 您将需要订阅Observable集合的CollectionChanged事件。 something like this should get you started.
这样的事情应该会让您入门。
ObservableCollection<Product> products = new ObservableCollection<Product>();
//Replace the lst with your control
var lst = new List<Product>();
products.CollectionChanged += (sender, eve) =>
{
switch (eve.Action)
{
case NotifyCollectionChangedAction.Add:
//add to your control here
lst.Add((Product) eve.NewItems[0]);
break;
case NotifyCollectionChangedAction.Remove:
//add implementation here
break;
case NotifyCollectionChangedAction.Replace:
break;
case NotifyCollectionChangedAction.Move:
break;
case NotifyCollectionChangedAction.Reset:
break;
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.