A place for spare thoughts


WPF 4.5 supports MarkupExtension in the event properties

Filed under: wpf — Ivan Danilov @ 08:22

At last! Since the beginning of MVVM pattern it was some kind of a miss in the framework: when control supports commands you have more or less convenient model of work – create command in the ViewModel and bind control’s action to it. But if you want to use some event as a trigger for command – you can say goodbye to simplicity and conciseness of your code.

The cleanest approach I am aware of is to declare some attached behavior that will act as a bridge between control’s events (as it will subscribe and unsubscribe when needed) and ViewModel’s commands (receive command instance from binding and invoke it when event is raised). This approach has one major downside – you need to write new attached behavior for each control/event pair you need to use. It is easy, boring and error-prone.

For RoutedEvents there’s better option as we have some means to pinpoint event’s tail directly in XAML with compile-time checking. I won’t stop on it in details as you could read about it here and here or here.

There’s of course ‘heavy artillery’ variant in form of reflection (make attached behavior and specify event name as string), but it doesn’t have compile-time checking, IntelliSense and many other useful things so I don’t consider it a viable approach at all in day-to-day work.

With WPF 4.5 things are finally became better. Now you can write MarkupExtensions for events thus the main problem of the first approach (namely: impossibility to specify event in the clean manner for attached behavior) is defeated. Jonathan Antoine already wrote an example how to use this new feature here and here. He is showing only general technique, but replacing method call with command invocation seems almost trivial. What I’d like to have is something as below code:

<Grid PreviewMouseDown="{custMarkup:InvokeCommand Command={Binding MyCommand}, CommandParameter=Whatever}" />


  1. Thank you for the links to my posts 🙂

    Comment by jmix90 — 03/11/2011 @ 12:56

    • Thank you for good posts that is useful to link to 🙂

      Comment by sparethought — 03/11/2011 @ 13:02

  2. Klaus R. aus Neunkirchen (17.03.2015): Ich habe heute erst erfahren, dass ich einen zu hohen Blutdruck
    habe, aber eine natürliche Methode befürworte ich.

    Comment by Gesundheit Who — 12/04/2016 @ 15:17

  3. Die Menschen auf Okinawa leben seit jeher so,
    wie die modernen Ernährungspäpste uns predigen: weniger Fleisch, kleine Portionen, langsam

    Comment by ezangl78.blogspot.com — 29/04/2016 @ 13:19

  4. Wer sein fahrtechnisches Können noch etwas perfektionieren möchten, kann dies tun, indem er an einem der
    regelmäßig angebotenen Fahrtrainings teilnimmt.

    Comment by Bennett — 20/06/2016 @ 07:01

  5. Zudem ist es in südlichen Länder mittags oft so heiß, dass man mehr Lust auf einen Snack als auf ein deftiges Essen hat.

    Comment by ezangl78.tumblr.com — 02/08/2016 @ 04:27

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: