MouseBinding Class

Binds a MouseGesture to a RoutedCommand (or another ICommand implementation).

Namespace:  System.Windows.Input
Assembly:  PresentationCore (in PresentationCore.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

public class MouseBinding : InputBinding
<MouseBinding .../>

A MouseBinding associates a MouseGesture with an ICommand implementation. RoutedCommand is the primary implementation of the ICommand interface for the WPF commanding system. In general, when the MouseGesture is executed, the command is invoked, although the command behavior is further influenced by command-specific factors such as the CanExecute value. For more information on commanding, see the Commanding Overview.

When defining a MouseBinding in Extensible Application Markup Language (XAML), there are two ways to specify the MouseGesture. The first way to establish a MouseBinding in XAML is to define the Gesture attribute of the MouseBinding element, which enables a syntax to specify mouse actions and modifiers as a single string; for example, "CTRL+LeftClick". The second way is to define the MouseAction attribute of the MouseBinding element. Both ways of setting the MouseGesture are equivalent and modify the same underlying object, but there will be a conflict if both are used. In the case when the MouseAction and the Gesture attributes are both set, the gesture is invalid. In general, it is recommended that you use only the Gesture attribute from XAML, even if you do not specify modifiers; this avoids ambiguity, provides the most streamlined syntax, and provides the most straightforward representation for serialization.

A MouseBinding can be defined on a specific object or at the class level by registering a RegisterClassInputBinding on the CommandManager class. The most typical way to define a MouseBinding is within the control template, setting the InputBindings property in XAML by declaring one or more MouseBinding or KeyBinding elements.

The following example shows how to use a MouseBinding to bind a MouseGesture to a RoutedCommand.

<MouseBinding Gesture="Control+WheelClick"
              Command="ApplicationCommands.Open" />
MouseGesture OpenCmdMouseGesture = new MouseGesture();
OpenCmdMouseGesture.MouseAction = MouseAction.WheelClick;
OpenCmdMouseGesture.Modifiers = ModifierKeys.Control;

MouseBinding OpenCmdMouseBinding = new MouseBinding();
OpenCmdMouseBinding.Gesture = OpenCmdMouseGesture;
OpenCmdMouseBinding.Command = ApplicationCommands.Open;

this.InputBindings.Add(OpenCmdMouseBinding);

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2015 Microsoft