In Silverlight, the mouse wheel works very well with every main browser (Internet Explorer, Firefox, Chrome, etc.) but not when Windowless is enabled. When this feature is enabled, NAPI based browsers like Chrome or Firefox don’t allow Silverlight to manage the mouse wheel. This article explains how to get the mouse wheel event using DOM.
This article is based on code published on Compiled Experience but I’ve added some modifications to consider elements inherited from ItemsControl.
To use this behavior easily in your application, we’re going to create a Silverlight “Behavior” which will be added to your ListBox, ComboBox, etc.
Code is separated into 2 parts :
The “helper” code used to get information about wheel in DOM.
The behavior which calls the “helper” and which will be added to XAML.
public class MouseWheelEventArgs : EventArgs
{
public Point Location { get; private set; } public double Delta { get; private set; }
public bool Handled { get; set; }
public MouseWheelEventArgs(double delta)
: this(delta, new Point())
{
}
public MouseWheelEventArgs(double delta, Point location)
{
Delta = delta;
Location = location;
}
}
public class MouseWheelHelper
{
private static Worker MouseWheelWorker;
private bool isMouseOver;
public MouseWheelHelper(UIElement element)
{
if (MouseWheelWorker == null)
MouseWheelWorker = new Worker();
MouseWheelWorker.Moved += HandleMouseWheel;
element.MouseEnter += HandleMouseEnter;
element.MouseLeave += HandleMouseLeave;
element.MouseMove += HandleMouseMove;
}
public event EventHandler<MouseWheelEventArgs> Moved;
private void HandleMouseWheel(object sender, MouseWheelEventArgs args)
{
if (isMouseOver)
Moved(this, args);
}
private void HandleMouseEnter(object sender, EventArgs e)
{
isMouseOver = true;
}
Read more: Aymeric's blog