#676 – MouseWheel Event Is Fired for Element That Mouse Pointer Is Over

When the user rotates the mouse wheel, the PreviewMouseWheel and MouseWheel events will fire.  The PreviewMouseWheel event tunnels down the logical tree, firing for the topmost element and working down to the element that the mouse pointer was located over when the mouse wheel was rotated.  The MouseWheel event then fires, bubbling up the logical tree and firing for each element until the topmost element is reached.

In the example below, if the user rotates the mouse wheel while the mouse pointer is over the Button, the event sequence is:

  • PreviewMouseWheel for Window
  • PreviewMouseWheel for StackPanel
  • PreviewMouseWheel for Button
  • MouseWheel for  Button
  • MouseWheel for StackPanel
  • MouseWheel for Window
<Window Name="win1" x:Class="WpfApplication1.MainWindow"
        Title="Mouse Wheel"
        Width="350" Height="200"
        MouseWheel="Window_MouseWheel" PreviewMouseWheel="Window_PreviewMouseWheel">

    <StackPanel MouseWheel="StackPanel_MouseWheel" PreviewMouseWheel="StackPanel_PreviewMouseWheel">
        <Label Content="Tiberius Julius Caesar Augustus" />
        <Button Content="Make Me Emperor" MouseWheel="Button_MouseWheel" PreviewMouseWheel="Button_PreviewMouseWheel"/>


#675 – Handling the PreviewMouseWheel and MouseWheel Events

Every user interface element defines the PreviewMouseWheel/MouseWheel event pair, which fire whenever the user interacts with the mouse wheel.

The PreviewMouseWheel event is a tunneling event, firing on each element of the logical tree, from the top down.  The corresponding MouseWheel event is bubbling, firing up the logical tree from the control where the event originated.

The mouse wheel events will fire once for each click of the mouse wheel.  The MouseWheelEventArgs.Delta property is meant to indicate the amount that the mouse wheel changed, but will in practice take on one of two values–a positive value when the mouse wheel is rotated forward (away from the user) and a negative value otherwise.

        private static int eventCount = 0;

        private void win1_MouseWheel(object sender, MouseWheelEventArgs e)
            Info = string.Format("Click #{0}, Delta = {1}", ++eventCount, e.Delta);

Rotating the wheel forward, we get a Delta of 120.

Rotating backward, we get -120.