数据绑定ListView多选和过滤
我有一个数据绑定项目列表。
我有一个文本框,可以通过将其可见性与描述是否包含键入的文本绑定来动态过滤它们。这也绑定到“focusable”属性,以删除由于过滤器而不可见的选定项目。
ListBoxItems 的 DataTrigger:
<DataTrigger Value="False">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource filterToBoolean}">
<Binding Path="Description" />
<Binding ElementName="txtFilter" Path="Text" />
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Visibility" Value="Collapsed" />
<Setter Property="Focusable" Value="False" />
</DataTrigger>
我的问题出现在以下场景中。假设我们有三个项目(Joe、Bob、Jacob)。
如果我们在过滤器中输入“J”,然后选择“Joe”并按住 Shift 键并单击“Jacob”,即使“Bob”不可见,也会选择三个项目(全部)。如果在应用过滤器之前选择了所有三个,则同样适用。
我在这里找到了一个尝试解决此问题的示例,但是它并不完全有效。如果在应用过滤器之前选择项目,它将适用,但是,如果在应用过滤器之后选择项目,则会出现相同的问题。
在此先感谢您的任何帮助。
I have a list of of databound items.
I have a textbox that filters them on the fly by binding their visibility to whether or not the description contains the typed text. This also is bound to the 'focusable' property to remove selected items that are not visible because of the filter.
The DataTrigger for the ListBoxItems:
<DataTrigger Value="False">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource filterToBoolean}">
<Binding Path="Description" />
<Binding ElementName="txtFilter" Path="Text" />
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Visibility" Value="Collapsed" />
<Setter Property="Focusable" Value="False" />
</DataTrigger>
My problem shows up in the following scenario. Say we have three items (Joe, Bob, Jacob).
If we type 'J' in the filter and then select 'Joe' and Shift+Click 'Jacob' there will be three items selected (all of them) even though 'Bob' is not visible. The same applies if all three were selected before the filter was applied.
I have found an example here that attempts to fix this, but, it doesn't completely work. It'll apply if the items are selected before the filter is applied, but, if selected afterwards the same problem arises.
Thanks in advance for any help here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ListView
将为集合中的每个对象创建一个ListViewItem
。仅仅因为可见性设置为隐藏并不意味着该项目不在列表视图中。当您按住 Shift 键选择项目时,它会选择两者之间的所有项目 - 其中包括“Bob”(可见或不可见)。为了正确的集合过滤,您应该使用
CollectionViewSource
的Filter
属性。如何完成此操作的示例是此问题中接受的答案:Trigger Filter on CollectionViewSource编辑
过滤速度缓慢的原因有很多。这个问题对您可以检查的事情有一些建议:WPF's ICollectionView.filter with大量数据
如果这没有帮助,那么可能会问另一个问题(您应该提供如何进行过滤和列表视图数据绑定的代码)。
The
ListView
will create aListViewItem
for each object in your collection. Just because the visibility is set to hidden doesn't mean that the item is not in the listview. When you Shift-Select items it selects all items between the two - which includes "Bob" (visible or not).For proper collection filtering you should use the
Filter
attribute of theCollectionViewSource
. A sample of how it can be done is the accepted answer in this question: Trigger Filter on CollectionViewSourceEdit
There are many reasons why your filtering might be slow. This question has some suggestion for things you can check: WPF's ICollectionView.filter with large sets of data
If that does not help then maybe ask another question on SO (you should provide the code of how you do the filtering and listview databinding).