Recommended

  • Silverlight 4 Podcast Pack with Tim Heuer
  • Building Modular Silverlight Applications
  • Prism -  10 Things to Know
  • Securing Silverlight Application and WCF Service using ASP.Net Authentication Techniques
  • Model– View – ViewModel in Silverlight
Skip Navigation LinksHome / Tips / View Tip

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

+ Add to SilverlightShow Favorites
3 comments   /   posted by Denislav Savkov on Sep 18, 2008
(1 votes)
Categories: Styles and Templates

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!

Share


Comments

Comments RSS RSS
  • 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

 
 

   
  
  
   
Please add 6 and 6 and type the answer here: