WPF:ListBox 或Panel 负责鼠标滚轮导航吗?

发布于 2024-07-15 02:57:13 字数 261 浏览 12 评论 0原文

我有一个自定义列表框,它使用自定义面板作为 ItemsHost。 我想控制鼠标滚轮输入,以便转动滚轮更改单个选定的项目。

我相信最好的方法是处理 OnPreviewMouseWheel 事件(尽管这只是我想要的,因为它不提供水平滚轮数据)。

现在的大问题是:是否有处理 OnPreviewMouseWheel 的最佳实践? 在ListBox(默认情况下不知道Panel 子元素的排列)或Panel 中(默认情况下不知道其子元素的“IsSelected”属性)?

I have a custom ListBox which uses a custom Panel as ItemsHost. I want to have control over mouse wheel input, so that turning the wheel changes the single selected item.

I believe that the best method to do so is to handle the OnPreviewMouseWheel event (although this is only have what I want since it doesn't provide horizontal wheel data).

Now the big question: Is there a best practice where to handle OnPreviewMouseWheel? In ListBox (which by default doesn't have a clue about the arrangement of the Panel's child elements) or in Panel (which by default doesn't have a clue about the "IsSelected" property of it's child elements)?

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

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

发布评论

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

评论(2

深海少女心 2024-07-22 02:57:13

我认为您应该从 ListBox 执行此操作。 ListBox 继承自 选择器 处理与选择有关的所有事情。

鼠标滚轮选择行为可以应用于任何类型的Panel(首先使用标准ListBox实现它甚至可能是一个好主意),并且您可能想要使用Panel 位于 ListBox 之外的某个位置,其中选择逻辑毫无意义。

I think you should do this from the ListBox. ListBox inherits from Selector which handles everything to do with selection.

The mousewheel selection behavior could apply with any kind of Panel (it might even be a good idea to implement it using a standard ListBox first), and you might want to use the Panel somewhere outside of a ListBox where the selection logic would make no sense.

爱殇璃 2024-07-22 02:57:13

它应该从列表框完成,因为只有它知道当前选择的项目。 面板不想也不需要知道它是否被选择。

我建议将其实现为附加行为,以便您可以多次重复使用该功能。

为此:
- 创建一个新类(可能称为 ListBoxSelector),并附加一个名为 MouseWheelChangesSelection(true/false)的属性。
- 添加 PropertyNotifyChangedEvent,并在属性更改时为 PreviewMouseWheel / MouseWheel 事件注册事件侦听器。

您可以通过以下任一方式更改当前选定的项目:
- 增加所选索引; 或
- 使用集合视图源的上一个/下一个移动

It should be done from the ListBox as only it knows about what item is currently selected. The panel doesn't want or need to know if it selected or not.

I would recommend implementing this as an attached behavior so you can re-use the functionality multiple times.

To do this:
- create a new class (maybe called ListBoxSelector) with an attached property called MouseWheelChangesSelection (true/false).
- add a PropertyNotifyChangedEvent and when the property is changed register an event listener for the PreviewMouseWheel / MouseWheel events.

You could change the currently selected item by either:
- incrementing the selected index; or
- using the collection view source's move prev/next

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