Posts Tagged ‘ Mouse ’

WPF MouseDown / MouseUp Command

Maybe you wanted to bind a command to the MouseDown Event of the Button.
But there is just the Command for the Click Event. Therefore it is necessary to do a little bit of coding.

I usually need the MouseDown / MouseUp Events of the Button for several use cases.
Therefore I just created a simple behavior which contains two command properties (on for the MouseUp and one for the MouseDown event).

The MouseCommandBehavior class looks like this

public static class MouseCommandBehavior
  {
    #region Commands

    ///
    /// The comamnd which should be executed when the mouse is down
    ///
    public static readonly DependencyProperty MouseDownCommandProperty =
        DependencyProperty.RegisterAttached("MouseDownCommand",
            typeof(ICommand),
            typeof(MouseCommandBehavior),
            new FrameworkPropertyMetadata(null, (obj, e) => OnMouseCommandChanged(obj, (ICommand)e.NewValue, false)));

    ///
    /// Gets the MouseDownCommand property
    ///
    public static ICommand GetMouseDownCommand(DependencyObject d)
    {
      return (ICommand)d.GetValue(MouseDownCommandProperty);
    }

    ///
    /// Sets the MouseDownCommand property
    ///
    public static void SetMouseDownCommand(DependencyObject d, ICommand value)
    {
      d.SetValue(MouseDownCommandProperty, value);
    }

    ///
    /// The comamnd which should be executed when the mouse is up
    ///
    public static readonly DependencyProperty MouseUpCommandProperty =
        DependencyProperty.RegisterAttached("MouseUpCommand",
            typeof(ICommand),
            typeof(MouseCommandBehavior),
            new FrameworkPropertyMetadata(null, new PropertyChangedCallback((obj, e) => OnMouseCommandChanged(obj, (ICommand)e.NewValue, true))));

    ///
    /// Gets the MouseUpCommand property
    ///
    public static ICommand GetMouseUpCommand(DependencyObject d)
    {
      return (ICommand)d.GetValue(MouseUpCommandProperty);
    }

    ///
    /// Sets the MouseUpCommand property
    ///
    public static void SetMouseUpCommand(DependencyObject d, ICommand value)
    {
      d.SetValue(MouseUpCommandProperty, value);
    }

    #endregion

    ///
    /// Registeres the event and calls the command when it gets fired
    ///
    private static void OnMouseCommandChanged(DependencyObject d, ICommand command, bool isMouseUp)
    {
      if (command == null) return;

      var element = (FrameworkElement)d;

      if(isMouseUp)
          element.PreviewMouseUp += (obj, e) => command.Execute(null);
      else
          element.PreviewMouseDown += (obj, e) => command.Execute(null);
      }
    }
  }

I hardcoded in this class these two events. But you could even add your own ones 🙂

In the XAML-Code you can use it like that

        <button>


If you want a even more flexible approach where you can register just one Command then you can use the behavior of Sacha Barber.
Check this out WPF : Attached Commands

I attached a zip file (remove the .doc extension) which contains a complete project. It is slightly different then what I explained above but the idea is the same.

WpfApplication2.zip