可观察集合的多重观点 [英] Multiple Views of Observable Collection
问题描述
我创建,填充和绑定一个可观察集合像这样:
Dim _ObservableWEI As New ObservableWEI
...
_ObservableWEI.Add(New WEI()With {.WEInum = 1,.WEIvalue = 1})
_ObservableWEI.Add(新WEI()With {.WEInum = 2,.WEIvalue = 0})
_ObservableWEI.Add {.WEInum = 3,.WEIvalue = 2})
...
lbxAll.ItemsSource = _ObservableWEI
这很好。我现在需要第二个列表框包含集合的过滤版本。过滤器函数将WEIvalue = 1的元素抽出。
Dim view As ListCollectionView
...
view = CType(CollectionViewSource.GetDefaultView(_ObservableWEI),ListCollectionView)
view.Filter =新的Predicate(Of Object)(AddressOf ListFilter)
...
lbxView.ItemsSource = view
问题是过滤器影响两个列表框的内容。我想我需要一个特定的集合的实例来应用过滤器太多,但我不知所措!
感谢您的任何帮助。
我认为问题是你绑定到默认视图,当你改变这个,你改变了视图的一切绑定到相同的集合。从文档CollectionViewSource.GetDefaultView :
所有集合都有一个默认的CollectionView。 WPF总是绑定到一个视图,而不是一个集合。如果直接绑定到集合,WPF实际上会绑定到该集合的默认视图。这个默认视图被与集合的所有绑定共享,这将导致与集合的所有直接绑定共享一个默认视图的排序,过滤器,组和当前项目特性。
Collection和CollectionView的设计模式是有一个集合,但有多个视图。所以我想你需要做的是在它上面创建两个不同的集合视图对象:
Dim view1当新的ListCollectionView(_ObservableWEI )
'设置筛选,分组等等
'绑定到它
lbxAll.ItemsSource = view1
Dim view2作为新的ListCollectionView(_ObservableWEI)
'设置筛选,分组等等
'绑定到它
lbxView.ItemsSource = view2
I've been working on this problem for a while and I'm clearly missing something...
I create, populate and bind an observable collection like so:
Dim _ObservableWEI As New ObservableWEI
...
_ObservableWEI.Add(New WEI() With {.WEInum = 1, .WEIvalue = 1})
_ObservableWEI.Add(New WEI() With {.WEInum = 2, .WEIvalue = 0})
_ObservableWEI.Add(New WEI() With {.WEInum = 3, .WEIvalue = 2})
...
lbxAll.ItemsSource = _ObservableWEI
Which is fine. I now need a second listbox containing a filtered version of the collection. The filter function pulls out the elements with a WEIvalue = 1.
Dim view As ListCollectionView
...
view = CType(CollectionViewSource.GetDefaultView(_ObservableWEI), ListCollectionView)
view.Filter = New Predicate(Of Object)(AddressOf ListFilter)
...
lbxView.ItemsSource = view
The problem is the filter effects the contents of both listboxes. I guess I need a specific instance of the collection to apply the filter too or something but I'm at a loss!
Thanks for any help.
I think the problem is that you're binding to the default view, and when you change this, you're changing the view for everything bound to the same collection. From the docs for CollectionViewSource.GetDefaultView:
All collections have a default CollectionView. WPF always binds to a view rather than a collection. If you bind directly to a collection, WPF actually binds to the default view for that collection. This default view is shared by all bindings to the collection, which causes all direct bindings to the collection to share the sort, filter, group, and current item characteristics of the one default view.
The design pattern for Collection and CollectionView is that you have one collection, but multiple views. So I think what you need to do is to make two different collection view objects on it:
Dim view1 As new ListCollectionView(_ObservableWEI)
'set filtering, grouping, etc.
'bind to it
lbxAll.ItemsSource = view1
Dim view2 As new ListCollectionView(_ObservableWEI)
'set filtering, grouping, etc.
'bind to it
lbxView.ItemsSource = view2
这篇关于可观察集合的多重观点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!