WPF:ListBox 或Panel 负责鼠标滚轮导航吗?
我有一个自定义列表框,它使用自定义面板作为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您应该从
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 standardListBox
first), and you might want to use thePanel
somewhere outside of aListBox
where the selection logic would make no sense.它应该从列表框完成,因为只有它知道当前选择的项目。 面板不想也不需要知道它是否被选择。
我建议将其实现为附加行为,以便您可以多次重复使用该功能。
为此:
- 创建一个新类(可能称为 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