[英]Binding to a filtered observable collection
I have an observable collection of type A
.我有一个可观察的
A
类型集合。
Class A
contains an enum - IsWhite
. Class
A
包含一个枚举 - IsWhite
。
My observable collection is called ACollection
.我的可观察集合称为
ACollection
。
I also have 2 datagrids, one that will have an itemssource
bound to ACollection
where the A
items have IsWhite
set to false, the other datagrid which is bound to the same collection but with IsWhite
set to true.我还有 2 个数据网格,一个将绑定到
ACollection
的itemssource
,其中A
项的IsWhite
设置为 false,另一个绑定到同一个集合但IsWhite
设置为 true 的数据网格。
How can I achieve this?我怎样才能做到这一点?
The collection is declared as follows;该集合声明如下;
ObservableCollection<A> ACollection = new ObservableCollection<A>;
and the class和 class
public class A
{
IsWhite isWhiteEnum { get; set; } = IsWhite.False;
}
I want one datagrid itemssource
to bind to ACollection
populating the items where IsWhite
is False
and the other datagrid itemssource
to bind to ACollection
popualting items where IsWhite
is True
.我希望一个 datagrid
itemssource
绑定到ACollection
填充IsWhite
为False
的项目,另一个 datagrid itemssource
绑定到ACollection
填充IsWhite
为True
的项目。
Here's a precis of the relevent parts of the article here:以下是本文相关部分的摘要:
https://social.technet.microsoft.com/wiki/contents/articles/26673.wpf-collectionview-tips.aspx https://social.technet.microsoft.com/wiki/contents/articles/26673.wpf-collectionview-tips.aspx
You don't want to filter the default view of a collection because that way your filter would apply to both datagrids.您不想过滤集合的默认视图,因为这样您的过滤器将应用于两个数据网格。
This bit of code gets two independent views:这段代码有两个独立的视图:
PeopleView = (CollectionView)new CollectionViewSource { Source = People }.View;
LevelsPeopleView = (CollectionView)new CollectionViewSource { Source = People }.View;
People is an observablecollection of person.人是人的可观察集合。
Both those views are collectionviews, eg.这两个视图都是集合视图,例如。
public CollectionView LevelsPeopleView { get; set; }
The views are bound in TwoCollectionViews.xaml eg视图绑定在 TwoCollectionViews.xaml 例如
<DataGrid ....
ItemsSource="{Binding PeopleView}"
And the article illustrates various filters such as the msdn approach:并且文章说明了各种过滤器,例如 msdn 方法:
private void ShowOnlyBargainsFilter(object sender, FilterEventArgs e)
{
AuctionItem product = e.Item as AuctionItem;
if (product != null)
{
// Filter out products with price 25 or above
if (product.CurrentPrice < 25)
{
e.Accepted = true;
}
else
{
e.Accepted = false;
}
}
}
Or much more sophisticated approaches.或者更复杂的方法。
You set a filter:您设置了一个过滤器:
LevelsPeopleView.Filter = FirstOfLevel_Filter;
If the view has already grabbed the data out that collectionview then nothing will happen.如果视图已经从collectionview 中抓取了数据,那么什么都不会发生。 You need to also do
你还需要做
LevelsPeopleView.Refresh();
This sort of filtering is quite inefficient and linq is better at large datasets.这种过滤效率很低,linq 在大型数据集上表现更好。 Still better is small datasets.
小数据集更好。 Unless your users really really like scrolling.
除非你的用户真的很喜欢滚动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.