Mouse-Wheel-Helper for Silverlight (upgraded)

3 באפריל 2009

Recently I’ve required to add Mouse-Wheel support in some of our Silverlight projects. Quickly discovered that unfortunately this feature isn’t supported in built-in events of Silverlight (at least in versions 1.0 and 2.0). After short search I found this post with sample code that solves this problem by attaching handlers to mouse events on html page.

I made some changes in “MouseWheelHelper” class and added “UIElementExtender” class that extends classes that derived from “UIElement” class in order to provide Mouse-Wheel support.

Here small example that uses “MouseWheelHelper” and “UIElementExtender”:
(use mouse wheel to zoom-in/out, also you can change image by pasting new url into texbox)

Some code samples:

Attach even handler “HandleMouseWheel” to event from HTML-Side (each web-browser has different event for mouse-wheel):

if (HtmlPage.BrowserInformation.UserAgent.Contains("Chrome"))
HtmlPage.Window.AttachEvent("onmousewheel", HandleMouseWheel);
else if (HtmlPage.BrowserInformation.UserAgent.Contains("Firefox"))
HtmlPage.Window.AttachEvent("DOMMouseScroll", HandleMouseWheel);
HtmlPage.Document.AttachEvent("onmousewheel", HandleMouseWheel);

“HandleMouseWheel” event handler (each web-browser has specific property for mouse-wheel-delta, method corrects some values):

private void HandleMouseWheel(object sender, HtmlEventArgs args)
double delta = 0;
ScriptObject eventObj = args.EventObject;

if (eventObj.GetProperty("wheelDelta") != null)
        delta = ((
double) eventObj.GetProperty("wheelDelta"));
        if (HtmlPage.Window.GetProperty("opera") != null)
            delta = -delta;
        else if (HtmlPage.BrowserInformation.UserAgent.Contains("Chrome"))
            delta /= 140;
            delta /= 120;
    else if (eventObj.GetProperty("detail") != null)
        delta = -((
double) eventObj.GetProperty("detail"));
        if (!HtmlPage.BrowserInformation.UserAgent.Contains("Macintosh"))
            delta /= 2;
     if (delta == 0 || Moved == null) return;
wheelArgs = new MouseWheelEventArgs(delta);
     if (Moved != null) Moved(this, wheelArgs);
(wheelArgs.Handled) args.PreventDefault();

Method-Extension for “UIElement”:

public static void RegisterMouseWheelHandler(this UIElement sender, EventHandler<MouseWheelEventArgs> handler){ /*…*/  }

Use of Method-Extension:

/* … */
// "MouseWheel" event handler, controls "zoom" of Image control
private void OnMouseWheel(object sender, MouseWheelEventArgs e)
    if (e == null) return;
    e.Handled = true;
    zoomDelta += e.Delta * 12;
    img.Margin = new Thickness(zoomDelta);

Source code for “MouseWheelHelper” can be downloaded from here .
Source code for example project can be downloaded from here .

I’m not responsible for any damage that may be caused by using this post or its content. Any materials in this post were provided AS IS without any changes and other meanings.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



  1. REné22 באוקטובר 2009 ב 3:55

    Can you tell me why it doesn't work in Chrome?

  2. Maxim2 בנובמבר 2009 ב 22:55

    It works fine in Chrome (move mice above the "Design Sales" image and scroll). Use attached example project for your tests.

  3. shashank19 בפברואר 2010 ב 20:14

    This is nice.
    Do you have a sample code for scrollviewer scrolling?


  4. icon files22 בספטמבר 2012 ב 10:47

    I regret, that I can not participate in discussion now. I do not own the necessary information. But this theme me very much interests.

    P.S. Please review Security Software Icon Pack from popavidi3