2010-06-04 33 views
1

ScrollViewer的MouseWheel事件僅在滾動條位於其軌道的末端(頂部或底部/左側或右側)時纔會觸發。 MouseWheel事件在介於兩者之間的任何位置時不會觸發。ScrollViewer在滾動時不觸發MouseWheel事件

有沒有人有任何線索如何捕捉滾動時,它是由鼠標滾輪造成的?

回答

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

+0

我沒問題捕獲鼠標滾輪滾動。我的具體問題是滾動事件不會觸發,直到達到兩個限制中的一個。兩者之間的一切都不會導致鼠標滾輪發射。 – beaudetious 2010-06-07 13:09:52

+0

上面的代碼在每個鼠標滾輪上都會觸發我(在Firefox中),而不僅僅是當拇指處於極限時。 – Chaitanya 2010-06-08 07:39:17

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

這對我有用,謝謝。我不明白爲什麼MouseWheel事件有時會被處理(當它不是頂部或底部時)。你能解釋一下嗎? – gitsitgo 2013-09-05 15:57:34

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); 
相關問題