How to prevent ScrollViewer from using MouseWheel event

倾然丶 夕夏残阳落幕 提交于 2019-12-25 04:39:30

问题


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; 

回答1:


Try to set:

e.Handled=true;



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/3642695/how-to-prevent-scrollviewer-from-using-mousewheel-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!