【发布时间】:2010-12-31 22:12:07
【问题描述】:
这段代码将帮助我描述我的问题:
public class Car
{
...
}
public class CarQueue : ObservableCollection<Car>
{
public IEnumerable Brands
{
get { return (from Car c in this.Items select c.Brand).Distinct(); }
}
}
好的,现在我有一个绑定到 DataGrid 的 CarQueue 类的实例。当我将 Car 对象添加到队列中时,数据网格会自行更新,但我还有一个绑定到不更新的“Brands”属性的列表框。下面是一个简单的代码序列来解释:
CarQueue cq = new CarQueue();
DataGrid1.ItemsSource = cq;
ListBox1.ItemsSource = cq.Brands; // all above done during window load
...
Car c;
cq.Add(c); // datagrid updates, but not listbox
列表框是否因为绑定到具有动态 LINQ 查询的属性而没有更新?
我尝试的另一件事是继承 INotifyPropertyChanged 并向 CollectionChanged 事件添加新的事件处理程序(在我的 CarQueue 构造函数中):
this.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(CarQueue_CollectionChanged);
然后在事件处理程序中:
void CarQueue_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
PropertyChanged(this, new PropertyChangedEventArgs("Brands"));
}
这也不起作用。那么有人知道问题是什么吗?谢谢
【问题讨论】: