Click here to Skip to main content
11,928,987 members (57,522 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


17 bookmarked

Mouse Event Commands for MVVM

, 22 Jan 2013 CPOL
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

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
DongJin Kim10-Sep-15 14:44
memberDongJin Kim10-Sep-15 14:44 
Generalvery usefull Pin
Seifeddine198619-Dec-14 13:40
memberSeifeddine198619-Dec-14 13:40 
QuestionDoes not work with Button (instead of Rectangle) Pin
Member 105378903-Oct-14 2:58
memberMember 105378903-Oct-14 2:58 
AnswerRe: Does not work with Button (instead of Rectangle) Pin
Matt Searles19-Mar-15 17:18
memberMatt Searles19-Mar-15 17:18 
GeneralMy vote of 3 Pin
Member 1054639429-Jul-14 2:00
memberMember 1054639429-Jul-14 2:00 
GeneralRe: My vote of 3 Pin
Matthew Searles19-Aug-14 15:20
memberMatthew Searles19-Aug-14 15:20 
QuestionAmazing Pin
Member 1097545928-Jul-14 0:22
memberMember 1097545928-Jul-14 0:22 
AnswerRe: Amazing Pin
Matthew Searles19-Aug-14 15:21
memberMatthew Searles19-Aug-14 15:21 
Questionmissing png images? Pin
sbarnes3-Apr-14 0:01
membersbarnes3-Apr-14 0:01 
AnswerRe: missing png images? Pin
sbarnes3-Apr-14 0:07
membersbarnes3-Apr-14 0:07 
AnswerRe: missing png images? Pin
Matt Searles4-May-14 0:32
memberMatt Searles4-May-14 0:32 
Questionproject is not compiled Pin
Member 1019491420-Mar-14 21:02
memberMember 1019491420-Mar-14 21:02 
AnswerRe: project is not compiled Pin
MarkWardell19-Mar-15 4:11
memberMarkWardell19-Mar-15 4:11 
GeneralMy vote of 5 Pin
Wolfgang Polzleitner11-Mar-14 9:50
memberWolfgang Polzleitner11-Mar-14 9:50 
GeneralRe: My vote of 5 Pin
Matt Searles11-Mar-14 16:00
memberMatt Searles11-Mar-14 16:00 
QuestionBubbled events prevent this from working Pin
RickB_AZ1-Jan-14 6:32
memberRickB_AZ1-Jan-14 6:32 
AnswerRe: Bubbled events prevent this from working Pin
selfwalker27-Mar-14 17:39
memberselfwalker27-Mar-14 17:39 
SuggestionGarbage Collection? Pin
haindl17-Oct-12 22:39
memberhaindl17-Oct-12 22:39 
GeneralRe: Garbage Collection? Pin
Dakko18-Oct-12 1:49
memberDakko18-Oct-12 1:49 
GeneralRe: Garbage Collection? Pin
haindl18-Oct-12 2:50
memberhaindl18-Oct-12 2:50 
GeneralRe: Garbage Collection? Pin
MarkWardell11-May-15 15:38
memberMarkWardell11-May-15 15:38 
AnswerRe: Garbage Collection? Pin
haindl12-May-15 3:15
memberhaindl12-May-15 3:15 
GeneralRe: Garbage Collection? Pin
Matt Searles20-Jun-15 15:02
memberMatt Searles20-Jun-15 15:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 22 Jan 2013
Article Copyright 2012 by Matthew Searles
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid