在 wpf 中的 itemscontrol 上禁用鼠标滚轮
我有一个用户控件,它有一个滚动查看器,然后是一堆子控件,如文本框、单选按钮和列表框等。我可以使用鼠标滚轮滚动父滚动查看器,直到鼠标落在列表框中,然后鼠标滚轮事件开始进入列表框。
有什么方法可以让列表框将这些事件发送回父控件吗?像这个问题一样从父控件内部删除列表框(鼠标滚轮在 ScrollViewer 的子控件上方时不起作用)不是解决方案。
我已经尝试过,
private void ListBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
e.Handled = true;
}
但这也不起作用。
谢谢
I have a usercontrol that has a scrollviewer, then a bunch of child controls like text boxes, radio buttons, and listboxes, etc inside of it. I can use the mouse wheel to scroll the parent scrollviewer until my mouse lands inside a listbox then, the mouse wheel events start going to the listbox.
Is there any way to have the listbox send those events back up to the parent control? Removing the listbox from within side the parent control like this question suggests (Mouse wheel not working when over ScrollViewer's child controls) isnt a solution.
I have tried
private void ListBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
e.Handled = true;
}
but that didnt work either.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这可以通过附加行为来完成。
其中
i
命名空间是:请注意,您将需要
Microsoft.Xaml.Behaviors.Wpf
nuget 包来使用它。This can be accomplished via attached behaviors.
Where the
i
namespace is:Note that you will need the
Microsoft.Xaml.Behaviors.Wpf
nuget package to use this.您引用的答案正是导致问题的原因,ScrollViewer 内的 ListBox(由 ScrollViewer 等组成)捕获 MouseWheel 事件并处理它,防止其冒泡,因此 ScrollViewer 不知道该事件曾经发生过。
使用以下极其简单的 ControlTemplate 为您的 ListBox 进行演示(请注意,其中没有 ScrollViewer,因此不会捕获 MouseWheel 事件)ScrollViewer 仍将随着鼠标在 ListBox 上滚动而滚动。
您确实可以选择在鼠标进入 ScrollViewer 时捕获鼠标,因此它会继续接收所有鼠标事件,直到释放鼠标为止,但是此选项将要求您将任何进一步的鼠标事件委托给 ScrollViewer 中包含的控件,如果您想要响应...以下 MouseEnter MouseLeave 事件处理程序就足够了。
然而,我提供的两种解决方法都不是真正优选的,我建议重新考虑您实际上想要做什么。如果您解释您在问题中想要实现的目标,我相信您会得到更多建议......
The answer you have referenced is exactly what is causing your problem, the ListBox (which is composed of among other things a ScrollViewer) inside your ScrollViewer catches the MouseWheel event and handles it, preventing it from bubbling and thus the ScrollViewer has no idea the event ever occurred.
Use the following extremely simple ControlTemplate for your ListBox to demonstrate (note it does not have a ScrollViewer in it and so the MouseWheel event will not be caught) The ScrollViewer will still scroll with the mouse over the ListBox.
You do have the option of capturing the mouse when it enters the ScrollViewer though so it continues to receive all mouse events until the mouse is released, however this option would require you to delgate any further mouse events to the controls contained within the ScrollViewer if you want a response...the following MouseEnter MouseLeave event handlers will be sufficient.
Neither of the workarounds I have provided are really preferred however and I would suggest rethinking what you are actually trying to do. If you explain what you are trying to achieve in your question I'm sure you will get some more suggestions...
我按照 Amanduh 的方法解决了在滚动查看器中但在 WPF 中使用多个数据网格时遇到的相同问题:
I followed Amanduh's approach to solve the same problem I had with multiple datagrids in a scrollviewer but in WPF:
正如 Simon 所说,标准 ListBox 模板中的 ScrollViewer 正在捕获事件。要绕过它,您可以提供自己的模板。
NoWheelScrollViewer 的实现非常简单。
然后,每当您希望列表框不处理鼠标滚轮时。
As Simon said, it's the ScrollViewer in the standard ListBox template that's catching the event. To bypass it you can provide your own template.
And the implementation for NoWheelScrollViewer is pretty simple.
Then, whenever you want a listbox to not handle the mouse wheel.
对我有用的一个简单解决方案是覆盖内部控制模板以删除滚动查看器(无论需要哪个),如下
所示
我有一个像这样的结构
ListView (a)
ListView(二)
我想将 (b) 的鼠标滚轮滚动到 (a),但希望保持 (c) 的鼠标滚轮滚动可用。
我只是像这样重写了 (b) 的模板。这使我能够将除(c)之外的(b)的内容冒泡到(a)。另外,我仍然可以滚动(c)的内容。如果我想删除(c),那么我必须重复相同的步骤。
A simple solution which worked for me is to override the inner control template to remove the scroll viewer (whichever required) like this
For example
I have a structure like this
ListView (a)
ListView (b)
I wanted to bubble the mouse wheel scroll of (b) to (a), however wanted to keep the mouse wheel scroll of (c) available.
I simply overridden the Template of (b) like this. This allowed me to bubble contents of (b) except (c) to (a). Also, I can still scroll the contents of (c). If i want to remove even for (c) then i have to repeat the same step.
我试图将 Simon Fox 的答案改编为 DataGrid。我发现模板隐藏了我的标题,并且我从未通过在 C# 中执行此操作获得 mouseLeave 事件。这最终对我有用:
I was trying to adapt Simon Fox's answer for a DataGrid. I found the the template hid my headers, and I never got the mouseLeave event by doing it in C#. This is ultimately what worked for me:
如果原始解决方案不起作用,则修改西蒙福克斯的解决方案:
A modified Simon Fox's solution if the original doesn't work:
您必须从 ScrollViewer 监听 PreviewMouseWheel(它可以工作),但不能从列表框监听。
You must listening PreviewMouseWheel from ScrollViewer (it works), but not from listbox.