在 wpf 中的 itemscontrol 上禁用鼠标滚轮 [英] disable mouse wheel on itemscontrol in wpf

查看:50
本文介绍了在 wpf 中的 itemscontrol 上禁用鼠标滚轮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有滚动查看器的用户控件,然后是一堆子控件,例如文本框、单选按钮和列表框等.我可以使用鼠标滚轮滚动父滚动查看器,直到我的鼠标落在列表框内,然后鼠标滚轮事件开始进入列表框.

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.

有没有办法让列表框将这些事件发送回父控件?像这个问题所建议的那样从父控件内部删除列表框 (鼠标滚轮在 ScrollViewer 的子控件上不起作用) 不是解决方案.

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.

我试过了

private void ListBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    e.Handled = true;
}

但这也没有用.

谢谢

推荐答案

您所引用的答案正是导致您出现问题的原因,您的 ScrollViewer 中的 ListBox(由 ScrollViewer 组成)捕获 MouseWheel 事件并处理它,防止它冒泡,因此 ScrollViewer 不知道该事件曾经发生过.

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.

使用以下极其简单的 ControlTemplate 为您的 ListBox 进行演示(注意其中没有 ScrollViewer,因此不会捕获 MouseWheel 事件)ScrollViewer 仍会在将鼠标悬停在 ListBox 上时滚动.

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.

<UserControl.Resources>
     <ControlTemplate x:Key="NoScroll">
         <ItemsPresenter></ItemsPresenter>
     </ControlTemplate>
</UserControl.Resources>

<ScrollViewer>
    <SomeContainerControl>
        <.... what ever other controls are inside your ScrollViewer>
        <ListBox Template="{StaticResource NoScroll}"></ListBox>
    <SomeContainerControl>
</ScrollViewer>

您确实可以选择在鼠标进入 ScrollViewer 时捕获鼠标,因此它会继续接收所有鼠标事件,直到释放鼠标,但是此选项需要您将任何进一步的鼠标事件委托给包含在ScrollViewer 如果您想要响应...以下 MouseEnter MouseLeave 事件处理程序就足够了.

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.

private void ScrollViewerMouseEnter(object sender, MouseEventArgs e)
{
    ((ScrollViewer)sender).CaptureMouse();
}

private void ScrollViewerMouseLeave(object sender, MouseEventArgs e)
{
    ((ScrollViewer)sender).ReleaseMouseCapture();
}

然而,我提供的两种解决方法都不是首选,我建议重新考虑您实际尝试做的事情.如果您在问题中解释了您要实现的目标,我相信您会得到更多建议...

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

这篇关于在 wpf 中的 itemscontrol 上禁用鼠标滚轮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆