Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
MouseBinding Class
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4.0

Other versions are also available for the following:
.NET Framework Class Library
MouseBinding Class

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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
Visual Basic (Declaration)
Public Class MouseBinding _
    Inherits InputBinding
Visual Basic (Usage)
Dim instance As MouseBinding
C#
public class MouseBinding : InputBinding
Visual C++
public ref class MouseBinding : public InputBinding
F#
type MouseBinding =  
    class
        inherit InputBinding
    end
XAML Object Element Usage
<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.

XAML
<MouseBinding Gesture="Control+WheelClick"
              Command="ApplicationCommands.Open" />
C#
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, 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: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker