防止更改 Filter 时 ListViewItem 上的 ContextMenu 消失

发布于 2024-11-29 20:53:17 字数 268 浏览 1 评论 0原文

我有一个 ListView 和 TextBox 用于输入搜索文本。

当用户搜索某些文本时,我启动一个长时间运行的搜索操作(在 UI 线程上,通过 UI 线程计时器),并且时不时地,当搜索仍然处于活动状态时,我会更改 ICollectionView.Filter 属性,使 ListView 刷新并显示搜索发现的更多匹配项。

问题是,如果用户右键单击其中一项,然后当搜索在后台进行时,树会刷新,然后上下文菜单就会消失。

我怎样才能防止这种情况发生?

I have a ListView and TextBox for entering search text.

When the user searches for some text, I start a long-running search operation (on the UI thread, through a UI-thread-timer), and every now and again, while the search is still active, I change the ICollectionView.Filter property, to cause the ListView to refresh and present more items that the search has found as matching.

The problem is that if the user right clicks on one of the items, and then as Search is happening in the background, the tree is refreshed, then the ContextMenu disappears.

How can I prevent that from happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

新一帅帅 2024-12-06 20:53:17

这是我的理论:
由于刷新了整个树,WPF 无法知道该项目(右键单击的项目)与刷新后的项目完全相同。从技术上讲,该项目甚至不再“存在”了。它实际上是一个具有完全相同属性的新项目(从 ListView 的角度来看)。

您可以尝试通过以下几种方法来修复此问题:

  • 捕获项目的右键单击,存储项目;捕获树刷新,检查该项目是否等于存储的项目;如果相等则显示ContextMenu。

  • 不是刷新整个树,而是将项目添加到列表末尾。

  • 当 ContextMenu 打开时,暂停刷新树。

Here's my theory:
Because the whole tree is refreshed, WPF cannot know that the item (which was right-clicked) is the exact same item as after the refresh. Technically, that Item isn't even "there" anymore. It's really a new item with the exact same properties (from the ListView's perspective).

There are a some ways you can try to remediate this:

  • Capture the right-click of the item, store the item; capture the tree refresh, check if the item is equal to the stored item; if equal show the ContextMenu.

  • Instead of refreshing the whole tree, add items to the end of the list.

  • While ContextMenu is open, suspend refreshing the tree.

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