Click here to Skip to main content
Click here to Skip to main content

Mouse Event Commands for MVVM

, 22 Jan 2013
Rate this:
Please Sign up or sign in to vote.
Use an Attached Property to execute an ICommand.


If you ever want to pass MouseEventArgs to a ViewModel, here's a neat way to do it Smile | <img src=

The Attached Property 

public class MouseBehaviour
    public static readonly DependencyProperty MouseUpCommandProperty =
        DependencyProperty.RegisterAttached("MouseUpCommand", typeof(ICommand), 
        typeof(MouseBehaviour), new FrameworkPropertyMetadata(
        new PropertyChangedCallback(MouseUpCommandChanged)));

    private static void MouseUpCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        FrameworkElement element = (FrameworkElement)d;

        element.MouseUp += new MouseButtonEventHandler(element_MouseUp);

    static void element_MouseUp(object sender, MouseButtonEventArgs e)
        FrameworkElement element = (FrameworkElement)sender;

        ICommand command = GetMouseUpCommand(element);


    public static void SetMouseUpCommand(UIElement element, ICommand value)
        element.SetValue(MouseUpCommandProperty, value);

    public static ICommand GetMouseUpCommand(UIElement element)
        return (ICommand) element.GetValue(MouseUpCommandProperty);

We simply register the attached property, hook the MouseUp event for the FrameworkElement, and invoke the Command in the handler. Simple enough, right? 


<Image Source="c:/temp.png" [Your xmlns]:MouseBehaviour.MouseUpCommand="{Binding MouseUpCommand}"></Image>

You do not, of course, have to use an <Image>, any framework element will work just fine. That's the beauty of Attached Properties! 

Having trouble attaching source, which contains Attached Properties for handling any mouse event MVVM style. But when it's up, includes 

  • MouseUp
  • MouseDown
  • MouseEnter
  • MouseLeave
  • MouseLeftButtonDown
  • MouseLeftButtonUp
  • MouseMove
  • MouseRightButtonDown
  • MouseRightButtonUp
  • MouseWheel


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Matthew Searles
Software Developer Lovatts Publications
Australia Australia
No Biography provided

Comments and Discussions

Questionmissing png images? Pinmembersbarnes2-Apr-14 23:01 
AnswerRe: missing png images? Pinmembersbarnes2-Apr-14 23:07 
AnswerRe: missing png images? PinmemberMatt Searles3-May-14 23:32 
Questionproject is not compiled PinmemberMember 1019491420-Mar-14 20:02 
GeneralMy vote of 5 PinmemberWolfgang Polzleitner11-Mar-14 8:50 
GeneralRe: My vote of 5 PinmemberMatt Searles11-Mar-14 15:00 
QuestionBubbled events prevent this from working PinmemberRickB_AZ1-Jan-14 5:32 
AnswerRe: Bubbled events prevent this from working Pinmemberselfwalker27-Mar-14 16:39 
SuggestionGarbage Collection? Pinmemberhaindl17-Oct-12 21:39 
GeneralRe: Garbage Collection? PinmemberDakko18-Oct-12 0:49 
GeneralRe: Garbage Collection? Pinmemberhaindl18-Oct-12 1:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 22 Jan 2013
Article Copyright 2012 by Matthew Searles
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid