Click here to Skip to main content
15,069,297 members
Articles / Desktop Programming / WPF
Posted 17 Oct 2012

Tagged as


22 bookmarked

Mouse Event Commands for MVVM

Rate me:
Please Sign up or sign in to vote.
4.95/5 (19 votes)
22 Jan 2013CPOL
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

GeneralMy vote of 5 Pin
sbarnes26-Jan-20 1:29
Membersbarnes26-Jan-20 1:29 
QuestionMy vote of 5 Pin
Member 35098766-Jun-17 5:02
MemberMember 35098766-Jun-17 5:02 
QuestionYou forgot about MouseEnter in code Pin
Member 121224679-Jan-17 23:02
MemberMember 121224679-Jan-17 23:02 
QuestionHow can i know the Rotation direction MouseWheel? Pin
Grigor Yeghiazaryan22-Jan-16 8:57
MemberGrigor Yeghiazaryan22-Jan-16 8:57 
GeneralMy vote of 5 Pin
DongJin Kim10-Sep-15 13:44
MemberDongJin Kim10-Sep-15 13:44 
Generalvery usefull Pin
Seifeddine198619-Dec-14 12:40
MemberSeifeddine198619-Dec-14 12:40 
QuestionDoes not work with Button (instead of Rectangle) Pin
Member 105378903-Oct-14 1:58
MemberMember 105378903-Oct-14 1:58 
AnswerRe: Does not work with Button (instead of Rectangle) Pin
Matt Searles19-Mar-15 16:18
MemberMatt Searles19-Mar-15 16:18 
GeneralMy vote of 3 Pin
Member 1054639429-Jul-14 1:00
MemberMember 1054639429-Jul-14 1:00 
GeneralRe: My vote of 3 Pin
Matthew Searles19-Aug-14 14:20
MemberMatthew Searles19-Aug-14 14:20 
QuestionAmazing Pin
Srikanth Pagadala27-Jul-14 23:22
MemberSrikanth Pagadala27-Jul-14 23:22 
AnswerRe: Amazing Pin
Matthew Searles19-Aug-14 14:21
MemberMatthew Searles19-Aug-14 14:21 
Questionmissing png images? Pin
sbarnes2-Apr-14 23:01
Membersbarnes2-Apr-14 23:01 
AnswerRe: missing png images? Pin
sbarnes2-Apr-14 23:07
Membersbarnes2-Apr-14 23:07 
AnswerRe: missing png images? Pin
Matt Searles3-May-14 23:32
MemberMatt Searles3-May-14 23:32 
Questionproject is not compiled Pin
Member 1019491420-Mar-14 20:02
MemberMember 1019491420-Mar-14 20:02 
AnswerRe: project is not compiled Pin
MarkWardell19-Mar-15 3:11
MemberMarkWardell19-Mar-15 3:11 
GeneralMy vote of 5 Pin
Wolfgang Polzleitner11-Mar-14 8:50
MemberWolfgang Polzleitner11-Mar-14 8:50 
GeneralRe: My vote of 5 Pin
Matt Searles11-Mar-14 15:00
MemberMatt Searles11-Mar-14 15:00 
QuestionBubbled events prevent this from working Pin
RickB_AZ1-Jan-14 5:32
MemberRickB_AZ1-Jan-14 5:32 
AnswerRe: Bubbled events prevent this from working Pin
selfwalker27-Mar-14 16:39
Memberselfwalker27-Mar-14 16:39 
SuggestionGarbage Collection? Pin
haindl17-Oct-12 21:39
Memberhaindl17-Oct-12 21:39 
What about Garbage Collection?
As far as I can see, the FrameworkElement never gets removed from memory because the EventHandler is permanently referencing it.

If (for example) the FrameworkElement is inside a DataTemplate or a ControlTemplate it might be instantiated many many times.
So you may have a problem in the resource management of your application sooner or later because of the memory leakes caused by the strong references.

Maybe you should extend your code to use WeakEvents.
(Good starting point:[^])
GeneralRe: Garbage Collection? Pin
Dakko18-Oct-12 0:49
MemberDakko18-Oct-12 0:49 
GeneralRe: Garbage Collection? Pin
haindl18-Oct-12 1:50
Memberhaindl18-Oct-12 1:50 
GeneralRe: Garbage Collection? Pin
MarkWardell11-May-15 14:38
MemberMarkWardell11-May-15 14:38 

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.