数据绑定ListView多选和过滤

发布于 2024-12-26 06:42:03 字数 1002 浏览 1 评论 0原文

我有一个数据绑定项目列表。

我有一个文本框,可以通过将其可见性与描述是否包含键入的文本绑定来动态过滤它们。这也绑定到“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我做我的改变 2025-01-02 06:42:03

ListView 将为集合中的每个对象创建一个 ListViewItem。仅仅因为可见性设置为隐藏并不意味着该项目不在列表视图中。当您按住 Shift 键选择项目时,它会选择两者之间的所有项目 - 其中包括“Bob”(可见或不可见)。

为了正确的集合过滤,您应该使用 CollectionViewSourceFilter 属性。如何完成此操作的示例是此问题中接受的答案:Trigger Filter on CollectionViewSource

编辑

过滤速度缓慢的原因有很多。这个问题对您可以检查的事情有一些建议:WPF's ICollectionView.filter with大量数据

如果这没有帮助,那么可能会问另一个问题(您应该提供如何进行过滤和列表视图数据绑定的代码)。

The ListView will create a ListViewItem 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 the CollectionViewSource. A sample of how it can be done is the accepted answer in this question: Trigger Filter on CollectionViewSource

Edit

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文