开发者

ScrollViewer doesn't fire MouseWheel event while Scrolling

开发者 https://www.devze.com 2023-01-02 13:39 出处:网络
The ScrollViewer\'s MouseWheel event will fire 开发者_JAVA技巧only when the scrollbar is at the end of it\'s track (either the top or bottom/left or right).The MouseWheel event does not fire when it\'

The ScrollViewer's MouseWheel event will fire 开发者_JAVA技巧only when the scrollbar is at the end of it's track (either the top or bottom/left or right). The MouseWheel event does not fire when it's anywhere in between.

Does anyone have any clue as to how to capture the scrolling when it's being caused by the mouse wheel?


You need to add the following code to capture the scrolling event

public MainPage()
     {
      InitializeComponent();
      HtmlPage.Window.AttachEvent("DOMMouseScroll", OnMouseWheel);
      HtmlPage.Window.AttachEvent("onmousewheel", OnMouseWheel);
      HtmlPage.Document.AttachEvent("onmousewheel", OnMouseWheel);
      }

private void OnMouseWheel(object sender, HtmlEventArgs args)
      {
      // Your code goes here
      }

Reference : http://blog.thekieners.com/2009/04/06/how-to-enable-mouse-wheel-scrolling-in-silverlight-without-extending-controls/

To actually get the full scrolling working properly (without messing with mousewheel events), see my answer to this question - How can I get the mouse wheel to work correctly with the Silverlight 4 ScrollViewer


The scroll viewer is actually firing the event. The event is being handled, therefore, the handler will not be called. The way around this is to use the AddHandler method to add the handler.

Instead of using the UIElement.MouseWheel Event, use the UIElement.AddHandler method, like this:

MyScrollViewer.AddHandler(FrameworkElement.MouseWheelEvent,
    delegate(object sender, MouseWheelEventArgs e)
    {
        //if e.Handled == true then the page was actually scrolled,
        // otherwise, the scrollviewer is either at the beginning or at the end
        if (e.Handled == true)
        {
            //Here, you can do what you need
        }
    },
true);


@davidle1234:

    public delegate void SVMouseWheelDelegate(object sender, MouseWheelEventArgs e);
    public SVMouseWheelDelegate SVMouseWheelHandler { get; set; }

    private void SVMouseWheelHandlerLogic(object sender, MouseWheelEventArgs e)
    {
        //if e.Handled == true then the page was actually scrolled,
        // otherwise, the scrollviewer is either at the beginning or at the end
        if (e.Handled == true)
        {
            //Here, you can do what you need
        }
    }

and use it like so:

MyScrollViewer.AddHandler(FrameworkElement.MouseWheelEvent, SVMouseWheelHandler, true);
0

精彩评论

暂无评论...
验证码 换一张
取 消