[英]Notifying WPF DataGrid on changes
我有一个绑定到ICollectionView
的DataGrid
(打开过滤器)。 更具体地说,我设置view.Filter = SomeFilteringFunction
,它使用public DateTime DateFrom { get... set... }
属性,也绑定到DatePicker
。
好吧,现在,当我更改DatePicker
时,绑定属性DateFrom
已正确更改,但 DataGrid 显然没有重新过滤。
如何通知DataGrid
更新自己的最正确方法是什么?
先感谢您!
詹姆士
您不应该直接绑定到 ICollectionView,而是绑定到源集合,然后将过滤器应用于 CollectionViewSource.GetDefaultView 返回的 ICollectionView。
<DataGrid ItemsSource="{Binding MyCollection}" />
// should raise INotityPropertyChange.PropertyChanged
public ObservableCollection<Entity> MyCollection { get; set; }
MyCollection = new ObservableCollection<Entity>(ctx.EntitySet));
ICollectionView view = CollectionViewSource.GetDefaultView(MyCollection);
view.Filter = SomeFilteringFunction;
然后当 DatePicker 的值发生变化时,您需要告诉 ICollectionView 进行更新。
ICollectionView view = CollectionViewSource.GetDefaultView(MyCollection);
view.Refresh();
您可以订阅PropertyChanged
事件(我假设您在类上实现了该事件)并刷新处理程序中的视图:
var view = CollectionViewSource.GetDefaultView(Collection);
if (view != null)
{
view.Refresh();
}
不过,不确定是否有更清洁的方法,但我非常肯定您需要在某一时刻进行Refresh
调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.