(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .
        Login with Facebook

Tip: How to capture mouse scroll / wheel event in Silverlight?

(1 votes)
Denislav Savkov
>
Denislav Savkov
Joined Feb 11, 2008
Articles:   14
Comments:   6
More Articles
3 comments   /   posted on Sep 18, 2008
Tags:   mouse , denislav-savkov
Categories:   General

Silverlight doesn't support mouse wheel event. There is a way to catch them if we ask to the browser for them. This workaround works well if the page with your Silverlight application fits in the browser. If the page is bigger in height a scroll appears. Then the whole content of the page is scrolled and the SL application doesn't receive the wheel event until one end is reached.

Hook up to the event. Different names are used in the different browsers.

C#

using System.Windows.Browser;
...
HtmlPage.Window.AttachEvent( "DOMMouseScroll", OnMouseWheel ); // Mozilla
HtmlPage.Window.AttachEvent( "onmousewheel", OnMouseWheel );
HtmlPage.Document.AttachEvent( "onmousewheel", OnMouseWheel ); // IE

This is how you get the wheel delta.

C#

private void OnMouseWheel( object sender, HtmlEventArgs args )
 {
     double mouseDelta = 0;
     ScriptObject e = args.EventObject;
     // Mozilla and Safari
     if ( e.GetProperty( "detail" ) != null )
     {
         mouseDelta = ( ( double )e.GetProperty( "detail" ) );
     }
     // IE and Opera
     else if ( e.GetProperty( "wheelDelta" ) != null )
         mouseDelta = ( ( double )e.GetProperty( "wheelDelta" ) );
 
     mouseDelta = Math.Sign( mouseDelta );
}

That's it!


Subscribe

Comments

  • -_-

    RE: Tip: How to capture mouse scroll / wheel event in Silverlight?


    posted by Shaji on May 13, 2009 02:30
    Cool...
    very usefull tip. Thanks!
  • -_-

    RE: Tip: How to capture mouse scroll / wheel event in Silverlight?


    posted by A on May 13, 2009 08:17
    Where and what I should past in my ListBox to make it scrollable?
  • -_-

    RE: Tip: How to capture mouse scroll / wheel event in Silverlight?


    posted by anakin on Jul 10, 2009 11:59
    greate! thanks.

Add Comment

Login to comment:
  *      *       
Login with Facebook