.NET Zone is brought to you in partnership with:

Jonathan creates software, mostly with C#, XAML, and HTML5/JS. He was awarded the Microsoft MVP in the "Client Application Development section in January 2011 Jon is a DZone MVB and is not an employee of DZone and has posted 23 posts at DZone. You can read more from them at their website. View Full User Profile

WPF 4.5 – Part 6 : Markup Extensions for Events

08.12.2012
| 2847 views |
  • submit to reddit

MarkupExtension00The first time I read about this new feature and its description, I just shook my head and thought : “what is this ?”! Then Rob Relyea, the ex-PM of the WPF team and Fabien Lavocat save my day via Twitter : “create a delegate in the provide value method“.

In this post, which is a part of a series on the WPF 4.5 new features, we will discover what means exactly “Markup Extensions for Events“.

What is it and what scenario it addresses ?


As pointed out by Rob, markup extension can now provide values for event in the XAML. In this case, this is a Delegate which has to be provided. The WPF framework by itself does not define a markup extension to be used for events.

This feature enable new scenario which will, in my opinion, steps on the Blend behavior's toes. Indeed, to be able to create this kind of markup extension is a dream for someone which want to trigger an action on a control when an event is raised. Instead of remembering which namespace to add and which behavior to use with which trigger a developer will just have to create a markup extension just like he would create a converter.

Of course there is a drawback, a major one: this is not supported by Blend. If you are as a fan of Blend as I am, you’ll continue to use behavior/triggers because it’s simply a drag & drop ahead !

So even if, in my opinion it won’t be over-used, this is still a great feature and an option to keep in mind.

How to use it ?

This time, it is not as obvious to uses as the other new features of WPF 4.5.

To be a MarkupExtension, a class has to inherit from MarkupExtesion and to implement the abstract ProvideValue method. It is called by the framework which provide an IServiceProvider object as a parameter.

This serviceProvider is a dependency resolver which you can use to retrieve a service named IProvideValueTarget. This one will then be used to obtain the property targeted by the MarkupExtension(you can get the Targeted object too with it).

This property is the Event’s accessor (the one which is called when you subscribe to it with the ‘+=’ syntax). Then, reflection has to be used to find the Type of the handler of the aimed event.

Once this is done, a Delegate can be created and returned as a provided value by this MarkupExtension. In the example below, the delegate handler is a method named ‘MyMarkupExtensionInternalHandler’ defined on the MarkupExtension itself.

public override object ProvideValue(IServiceProvider serviceProvider)
{
   IProvideValueTarget targetProvider = serviceProvider
      .GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
   if (targetProvider == null)
      throw new InvalidOperationException(@"The CallAction extension
           can't retrieved the IProvideValueTarget service.");
 
   var targetEventAddMethod = targetProvider.TargetProperty as MethodInfo;
 
   //Retrieve the handler of the event
   ParameterInfo[] pars = targetEventAddMethod.GetParameters();
   Type delegateType = pars[1].ParameterType;
 
   //Retrieves the method info of the proxy handler
   MethodInfo methodInfo = this.GetType()
     .GetMethod("MyMarkupExtensionInternalHandler",
      BindingFlags.NonPublic | BindingFlags.Instance);
 
   //Create a delegate to the proxy handler on the markupExtension
   Delegate returnedDelegate = Delegate
      .CreateDelegate(delegateType, this, methodInfo);
 
   return returnedDelegate;
}
 
void MyMarkupExtensionInternalHandler(object sender, EventArgs e)
{
        //here something can be performed.
}

A few good things to know when you create your own markup extensions:

  1. Throw InvalidOperationException when something bad happens,
  2. Don’t think that everything is initialized: it is not. This is especially true for the DataContext of the target,
  3. Always check that the objects you retrieve are not null especially the service obtained via the IServiceProvider argument.

More advanced example

I wanted to write a full example when I played with this feature yesterday but I found out it could be a post by itself. So if you want a full example, you can read this post describing how to invoke a method on the ViewModel / DataContext when an event is raised.;

Published at DZone with permission of Jon Antoine, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)