【发布时间】:2013-04-18 12:19:40
【问题描述】:
我有一个DataGrid,它通过Thread 每隔几秒更新一次数据。 DataGrid 需要提供 Column Header 排序、分组和过滤。
我目前有一个DataGrid 绑定到一个ICollectionView 并且ICollectionView 的源是一个ObservableCollection。从我在其他线程上阅读的内容来看,这似乎是实现此目的的好方法。
排序“有效”,但在更新ObservableCollection 后更新ICollectionView.Source 时它会丢失。我尝试在更新前保存SortDescriptions,并在更新完成后将其重新添加到ICollectionView。但结果是一样的。
有人可以指出我缺少什么吗?
编辑这里有一些代码...
查看 (XAML)
<DataGrid ItemsSource="{Binding CollectionView, Source={StaticResource ViewModel}}>
视图模型
public ICollectionView CollectionView
{
get
{
collectionViewSource.Source = dataColl;
if (SortDescriptions != null)
{
foreach (SortDescription sd in SortDescriptions)
{
collectionViewSource.View.SortDescriptions.Add(sd);
}
}
collectionViewSource.View.Refresh();
return collectionViewSource.View;
}
}
public ObservableCollection<SomeObject> DataColl
{
get { return dataColl; }
private set
{
this.dataColl= value;
OnPropertyChanged("CollectionView");
}
}
以下是每隔几秒更新一次数据的方法……
private void UpdateData()
{
while (true)
{
System.Threading.Thread.Sleep(mDataRefreshRate);
// SortDescriptions is a Property of the ViewModel class.
SortDescriptions = collectionViewSource.View.SortDescriptions;
ObservableCollection<SomeObject> wDataColl
= new ObservableCollection<SomeObject>();
//... Irrelevant code that puts the data in wDataColl ...
DataColl= wDataColl;
}
}
【问题讨论】:
-
可以添加一些你试过的源代码吗?
标签: c# wpf mvvm datagrid filtering