如何防止 ScrollViewer 使用 MouseWheel 事件

发布于 2024-09-17 00:52:05 字数 877 浏览 1 评论 0原文

我正在构建 SL 应用程序,用于在布局上进行缩放和平移。一切工作正常,除了当我使用鼠标滚轮放大时,在一些缩放滚动条开始使用鼠标滚轮之后,我可以滚动而不是缩放。如果我将滚动条放在末尾或开头,我只能再次缩放。如何防止scrollviewer使用鼠标滚轮?我希望变焦只能通过滚轮操作。先感谢您!

这是我缩放内容时的 MouseWheel 方法的代码:

protected override void OnMouseWheel(MouseWheelEventArgs e) 
    { 
        base.OnMouseWheel(e);             

        if (e.Delta > 0) 
        { 
            this.aniScaleX.To += 0.2; 
            this.aniScaleY.To += 0.2; 

            this.sbScale.Begin(); 
        } 
        else if (e.Delta < 0 && (this.aniScaleX.To > 1 && this.aniScaleY.To > 1)) 
        { 
            this.aniScaleX.To -= 0.2; 
            this.aniScaleY.To -= 0.2; 

            this.sbScale.Begin(); 
        } 

        Sizer.Width = Board.ActualWidth * (double)this.aniScaleX.To; 
        Sizer.Height = Board.ActualHeight * (double)this.aniScaleY.To; 

I'm building SL application for zooming and panning across the layout. Everything is working fine, except that when I zoom in using mouse wheel , after some zoom scrollbars start to use mouse wheel so after that I can scroll not zoom. I only can zoom again if I put scrollbars at the end or begining. How to prevent scrollviewer from using mouse wheel? I want that zoom only be operated by wheel. Thank you in advance!

Here is my code of MouseWheel method when I'm zooming content :

protected override void OnMouseWheel(MouseWheelEventArgs e) 
    { 
        base.OnMouseWheel(e);             

        if (e.Delta > 0) 
        { 
            this.aniScaleX.To += 0.2; 
            this.aniScaleY.To += 0.2; 

            this.sbScale.Begin(); 
        } 
        else if (e.Delta < 0 && (this.aniScaleX.To > 1 && this.aniScaleY.To > 1)) 
        { 
            this.aniScaleX.To -= 0.2; 
            this.aniScaleY.To -= 0.2; 

            this.sbScale.Begin(); 
        } 

        Sizer.Width = Board.ActualWidth * (double)this.aniScaleX.To; 
        Sizer.Height = Board.ActualHeight * (double)this.aniScaleY.To; 

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

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

发布评论

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

评论(3

巡山小妖精 2024-09-24 00:52:05

尝试设置:

e.Handled=true;

Try to set:

e.Handled=true;
当梦初醒 2024-09-24 00:52:05

MouseWheel 事件是冒泡事件
事件。这意味着如果多个
MouseWheel 事件处理程序是
注册一系列对象
亲子关系
对象树中的关系
每个事件都可能收到
该关系中的对象。这
冒泡的比喻表明
事件从源头开始并起作用
它沿着对象树向上。对于一个
冒泡事件,发送者可用
事件处理程序标识
处理事件的对象,而不是
必然是实际存在的对象
收到输入条件
发起了该活动。获取对象
发起事件的,使用
事件的 OriginalSource 值
数据。 http://msdn.microsoft .com/en-us/library/system.windows.uielement.mousewheel(VS.95).aspx


就我而言,ScrollViewer 之前总是收到事件,因为他位于可视化树的顶部。因此,我只是在鼠标滚轮事件的滚动查看器中注册了事件处理程序,并且总是在发生这种情况时,我只需将他重定向到执行缩放的“原始”鼠标滚轮功能。

我希望这能帮助那些像我一样被“困”在这里的人。谢谢大家的回答和建议..

The MouseWheel event is a bubbling
event
. This means that if multiple
MouseWheel event handlers are
registered for a sequence of objects
connected by parent-child
relationships in the object tree, the
event is potentially received by each
object in that relationship. The
bubbling metaphor indicates that the
event starts at the source and works
its way up the object tree. For a
bubbling event, the sender available
to the event handler identifies the
object where the event is handled, not
necessarily the object that actually
received the input condition that
initiated the event. To get the object
that initiated the event, use the
OriginalSource value of the event
data. http://msdn.microsoft.com/en-us/library/system.windows.uielement.mousewheel(VS.95).aspx

In my case,ScrollViewer always received event before because he is on the top of the visual tree. So I just registered event handler in scrollviewer on mouse wheel event and always when It happens, I simply redirect him to my "original" mousewheel function which do zoom.

I hope so that this will help somebody who is "stuck" like me here. Thank you all on your answers and suggestions..

违心° 2024-09-24 00:52:05

这花了我一段时间 (WPF),但基本上您应该在具有该事件的任何父 UIElement 中使用 AddHandler。

对我来说,我在主窗口中执行了此操作。所以我的代码看起来像:

public MainWindow()
{
    InitializeComponent();
    this.AddHandler(MainWindow.MouseWheelEvent, new RoutedEventHandler(this.MouseWheel_1), true);
}

这也意味着不重写 OnMouseWheelDown,而是创建一个与 RoutedEventHandler 委托匹配的方法,并将 e (这将是一个 RoutedEventArg)转换为 MouseWheelEventArgs 以访问确定缩放所需的属性。

我希望这对您的情况有所帮助。

This took me awhile (WPF), but basically you should use AddHandler in whatever parent UIElement that has the event.

For me, I did this in my MainWindow. So my code looked like:

public MainWindow()
{
    InitializeComponent();
    this.AddHandler(MainWindow.MouseWheelEvent, new RoutedEventHandler(this.MouseWheel_1), true);
}

This will also mean not overriding OnMouseWheelDown, but instead creating a method that matches the RoutedEventHandler delegate and casting e (which will be a RoutedEventArg) as MouseWheelEventArgs to get access to the properties required to determining zoom.

I hope this helps for your situation.

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