C# 在asp.net c中将可观察集合绑定到列表框
这是我的密码C# 在asp.net c中将可观察集合绑定到列表框,c#,asp.net,listbox,C#,Asp.net,Listbox,这是我的密码 ObservableCollection<product> pr = new ObservableCollection<product>(); protected void Page_Load(object sender, EventArgs e) { product p = new product(); p.PhoneMake = "Apple";
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;
您需要订阅可观察集合的CollectionChanged事件。 像这样的事情应该让你开始
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;
}
};
请共享您的xaml代码
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;
}
};