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

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

问题描述

我有一个用户控件,有一个ScrollViewer的,然后一堆子控件,如文本框,单选按钮,和列表框,等它里面。我可以使用鼠标滚轮滚动父ScrollViewer的,直到我的一个列表框里面鼠标的土地,然后,鼠标滚轮事件开始准备到列表框中。有没有什么办法让列表框中发送这些事件备份到父控件?从内部侧删除列表框这样的问题,家长控制建议(<一href="http://stackoverflow.com/questions/761243/wpf-mouse-wheel-not-working-when-over-scrollviewers-child-controls">http://stackoverflow.com/questions/761243/wpf-mouse-wheel-not-working-when-over-scrollviewers-child-controls)心不是一个解决方案。

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 (http://stackoverflow.com/questions/761243/wpf-mouse-wheel-not-working-when-over-scrollviewers-child-controls) isnt a solution.

我已经试过

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

但没有工作无论是。

but that didnt work either.

感谢

推荐答案

您参考答案究竟是什么造成您的问题,列表框(它是由一个ScrollViewer中的除其他事项外)您的ScrollViewer内捕获鼠标滚轮事件并处理它,$ P $冒泡,因此,ScrollViewer的不知道的情况下发生过它pventing

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你的列表框证明(注意它并没有一个ScrollViewer中的它,所以鼠标滚轮事件将不会被抓)的ScrollViewer中仍然会用鼠标在列表框滚动。

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,但这样它继续接收所有的鼠标事件,直到鼠标被释放的选项,但该选项将要求您delgate任何进一步的鼠标事件包含在控件的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();
}

无论是我所提供的解决方法真的是preferred不过,我会建议重新考虑你实际上是在做什么。如果你能解释一下你想实现你的问题是什么,我相信你会得到一些更多的建议...

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

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

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