Adding Mouse Wheel support in Silverlight. The easy way.

Posted by: the telerik blogs, on 24 Jul 2008 | View original | NEW Bookmarked: 0 time(s)

One of the best uses ofour Routed Event extension is implementing missing system events in Silverlight, such as MouseWheel and RightButtonUp/Down. The API we provide is the same as WPF, so when Silverlight eventually gets support for those events, most probably you will not have to change much. Other good thing is that you do not need to write any JavaScript, or to know what's going on beneath.The bad thing is that you will have toenable the windowless mode of the Silverlight plug-in, which will slightly decrease the performance of your application.

To attach a Routed Event handler for the MouseWheel eventon a ScrollViewer you need to add the following code in the constructor of your Silverlight Page:

Mouse.AddMouseWheelHandler(ScrollViewer1,newEventHandler<MouseWheelEventArgs>(OnMouseWheelHandler));

 

Additionaly, you will have to declare the OnMouseWheelHandler method:

privatestaticvoidOnMouseWheelHandler(objectsender,MouseWheelEventArgsargs)
{
if(e.Handled)
{
return;
}
ScrollViewerscrollViewer=senderasScrollViewer;
intscrollDelta=40*args.Delta/3;
if(scrollViewer.ComputedVerticalScrollBarVisibility==Visibility.Visible)
{
scrollViewer.ScrollToVerticalOffset(
Math.Min(scrollViewer.ScrollableHeight,
scrollViewer.VerticalOffset-scrollDelta));
e.Handled=true;
}
elseif(scrollViewer.ComputedHorizontalScrollBarVisibility==Visibility.Visible)
{
scrollViewer.ScrollToHorizontalOffset(
Math.Min(scrollViewer.ScrollableWidth,
scrollViewer.HorizontalOffset-scrollDelta));
e.Handled=true;
}
}

 

Unfortunately, the method above is not suitable for enhancing controls which have internal ScrollViewer, such as the standard ListBox, because you cannot get a reference to its ScrollViewer, in order to attach the Routed Event handler. In this case you could create a Class Handler for the ScrollViewer type:

EventManager.RegisterClassHandler(typeof(ScrollViewer),Mouse.MouseWheelEvent,newEventHandler<MouseWheelEventArgs>(OnMouseWheelHandler));

 

In this case,since itwill affect ALLScrollViewer controls on the page, the OnMouseWheelHandler method should be slightly different in order to avoid interference with the Telerik controls, which already have mouse wheel support:

privatestaticvoidOnMouseWheelHandler(objectsender,MouseWheelEventArgsargs)
{
if(e.Handled||!ListBox1.IsAncestorOf(scrollViewer))
{
return;
}
...
}

 

The check could be modified to exclude certain controls, e.g.if you have only a couple of Telerik controls on your page and lots ot other controls, containing ScrollViwers, you could disable the handler only for the Telerik controls and enhance all other ScrollViwers on the page.

The IsAncestorOf method is an extension method, that you could declare in some static class in your application as:

publicstaticboolIsAncestorOf(thisFrameworkElementtarget,FrameworkElementelement)
{
if(target==element)
{
returntrue;
}
FrameworkElementparent=VisualTreeHelper.GetParent(element)asFrameworkElement;
while(parent!=null)
{
if(parent==target)
{
returntrue;
}
parent=VisualTreeHelper.GetParent(parent)asFrameworkElement;
}
returnfalse;
}

 

Of course, the MouseWheel event is not limited to just scrolling ScrollViewer controls. For example, RadSlider and RadNumericUpDown use the MouseWheel event to change their values, you could zoom-in/out a DeepZoom image, etc. The possibilities are endless.

Attached is the source, containing the refactored code from the above. Happly MouseWheeling :)

Advertisement
Category: Silverlight | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 1137 | Hits: 9

Similar Posts

  • Supportable Customizations: Don't Panic more
  • Supportable Customizations: Don't Panic more
  • Drag and Drop with Managed Code more
  • Displaying XPS Documents in Silverlight more
  • VB On Silverlight more
  • Moonlight 1.0 Released, Silverlight script updated – and a Chrome hack more
  • LLBLGen Pro v2.6 has been released! more
  • Intersoft Solutions Releases WebUI Studio 2009 The Worlds Most Innovative Web Development Toolkit more
  • Telerik Launches RadControls for Silverlight 3 for Line-of-Business Application Development more
  • Telerik Announces Support for Microsoft Silverlight 3 more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD