过滤可观察集合
filtering observable collection
我是 wpf、Entity Framework6 和 LINQ 的新手。我有一个绑定到 CollectionViewSource 的数据网格,它绑定到 ObservableCollection。我只是按照 https://msdn.microsoft.com/en-us/data/jj574514 中的教程进行操作,并成功地从数据网格中添加、更新和删除。本教程展示了如何加载完整的 table(即类别):
private void Window_Loaded(object sender, RoutedEventArgs e)
{
System.Windows.Data.CollectionViewSource categoryViewSource =
((System.Windows.Data.CollectionViewSource)(this.FindResource("categoryViewSource")));
_context.Categories.Load();
categoryViewSource.Source = _context.Categories.Local;
}
现在我的问题很简单:如果我想加载部分类别而不是全部类别怎么办?我希望某些用户能够更新特定类别,因此网格将仅加载这些类别。
您需要使用过滤器。
categoryViewSource.View.Filter = item =>
{
Categories category = item as Categories; // hope this are your items
return category.Id > 10; // or put whatever condition you want
};
上述方法将return 仅显示 Id > 10 的类别(仅以 Id 为例)。
同时检查这个 link:
http://social.technet.microsoft.com/wiki/contents/articles/26673.aspx
如果你想使用linq,你可以这样试试:
ObservableCollection<Model> collection = new ObservableCollection<Model>();
ObservableCollection<Model> filteredCollection = new ObservableCollection<Model>(from item in collection where item.ID >0 && item.ID < 10 orderby item.ID select item);
如果我没记错的话,您可以将 observablecollection 直接绑定到 wpf 数据网格的 ItemsSource 属性。
如果你想了解更多关于linq的知识,我建议阅读https://msdn.microsoft.com/de-de/library/bb397933.aspx
我是 wpf、Entity Framework6 和 LINQ 的新手。我有一个绑定到 CollectionViewSource 的数据网格,它绑定到 ObservableCollection。我只是按照 https://msdn.microsoft.com/en-us/data/jj574514 中的教程进行操作,并成功地从数据网格中添加、更新和删除。本教程展示了如何加载完整的 table(即类别):
private void Window_Loaded(object sender, RoutedEventArgs e)
{
System.Windows.Data.CollectionViewSource categoryViewSource =
((System.Windows.Data.CollectionViewSource)(this.FindResource("categoryViewSource")));
_context.Categories.Load();
categoryViewSource.Source = _context.Categories.Local;
}
现在我的问题很简单:如果我想加载部分类别而不是全部类别怎么办?我希望某些用户能够更新特定类别,因此网格将仅加载这些类别。
您需要使用过滤器。
categoryViewSource.View.Filter = item =>
{
Categories category = item as Categories; // hope this are your items
return category.Id > 10; // or put whatever condition you want
};
上述方法将return 仅显示 Id > 10 的类别(仅以 Id 为例)。
同时检查这个 link: http://social.technet.microsoft.com/wiki/contents/articles/26673.aspx
如果你想使用linq,你可以这样试试:
ObservableCollection<Model> collection = new ObservableCollection<Model>();
ObservableCollection<Model> filteredCollection = new ObservableCollection<Model>(from item in collection where item.ID >0 && item.ID < 10 orderby item.ID select item);
如果我没记错的话,您可以将 observablecollection 直接绑定到 wpf 数据网格的 ItemsSource 属性。
如果你想了解更多关于linq的知识,我建议阅读https://msdn.microsoft.com/de-de/library/bb397933.aspx