筛选项目来源 [英] Filter itemsource
本文介绍了筛选项目来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用此代码,我将设置我的datagrid的Itemsource.但是我得到了更多的wpf控件,例如,从一个时间范围内,它需要更多的数据网格来进行过滤.我可以为此编写一个新查询,但由于数据已经可用,因此这似乎不必要,我只需要过滤它即可.但是什么是最好的方法呢?
任何帮助我都可以得到真正的感谢!
DateTime dateStart = CalenderSearch.SelectedDates.First();DateTime dateEnd = CalenderSearch.SelectedDates.Last();ObjectQuery< Fouten>fouten = eventsEntities.Foutens;var查询=(来自fouten的fout其中dateStart< = fout.Datum&dateEnd> = fout.Datum&fout.Rapporten.Treinen.NameTrein == trein.NameTreinorderby fout.Datum,fout.Time选择新的{基准= fout.Datum,时间= fout.Time,FoutCode = fout.FoutCode,Omschrijving = fout.Omschrijving,柜员= fout.Teller,模块= fout.Module,FoutId = fout.FoutId}).AsEnumerable().Select(x => gt new Fouten{基准= x.基准,时间= x.Time,FoutCode = x.FoutCode,Omschrijving = x.Omschrijving,柜员= x.Teller,模块= x.Module,FoutId = x.FoutId}).ToList();如果(query.Count == 0)foutensDataGrid.ItemsSource = null;别的foutensDataGrid.ItemsSource =查询;
解决方案
在WPF中,在GUI元素中用作 ItemsSource
的每个集合都具有 Filter
属性,其类型为 Predicate< object>
.
如果您设置此 Filter
并调用 解决方案
In WPF, each collection that are used as an ItemsSource
in a GUI element have an ICollectionView
associated with it.
ICollectionView
has a Filter
property, which is of type Predicate<object>
.
If you set this Filter
and call Refresh()
afterwards, the DataGrid will update itself to only show the items where Filter
returned true.
An example how you could use this:
var collectionView = CollectionViewSource.GetDefaultView(foutensDataGrid.ItemsSource);
collectionView.Filter = o => {
var fouten = o as Fouten;
//do your filtering, e.g.
return fouten.Datum <= dateEnd && fouten.Datum >= dateStart;
}
collectionView.Refresh();
这篇关于筛选项目来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文