滚动时ScrollViewer不会触发MouseWheel事件

时间:2010-06-04 17:46:07

标签: silverlight silverlight-4.0 scrollviewer

只有当滚动条位于其轨道的末尾(顶部或底部/左侧或右侧)时,ScrollViewer的MouseWheel事件才会触发。 MouseWheel事件在它之间的任何位置都不会触发。

有没有人知道如何在鼠标滚轮引起滚动时捕捉滚动?

3 个答案:

答案 0 :(得分:1)

您需要添加以下代码来捕获滚动事件

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
      }

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

要实际让滚动正常工作(不要弄乱鼠标滚轮事件),请参阅我对这个问题的回答 - How can I get the mouse wheel to work correctly with the Silverlight 4 ScrollViewer

答案 1 :(得分:1)

滚动查看器实际上正在触发事件。正在处理事件,因此不会调用处理程序。解决这个问题的方法是使用AddHandler方法添加处理程序。

不使用UIElement.MouseWheel Event,而是使用UIElement.AddHandler method,如下所示:

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);

答案 2 :(得分:0)

@ 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
        }
    }

并像这样使用它:

MyScrollViewer.AddHandler(FrameworkElement.MouseWheelEvent, SVMouseWheelHandler, true);
相关问题