WPF Extensibility Hacks – Preview 1 – Few interesting Triggers and Actions for WPF, Invoking Triggers based on Conditions etc

By Anoop Madhusudanan

Vote on HN

Some time back, I posted about the Silverlight Extensibility hacks Preview 2.0. I did a quick port of the code base to WPF under the name WEX or WPF Extensibility hacks, and now you have the goodness in .NET 3.5 + VS 2008


Just want to have a quick word on what is available. Here are a couple of points about Wex. Wex is built on top of System.Windows.Interactivity infrastructure.

  • Wex allows you to define multiple conditions for invoking triggers (like you can specify a KeyDown event trigger should be fired only if ‘A’ is pressed)
  • You can specify multiple conditions for invoking each action in a trigger
  • Wex introduces few more Triggers and Actions that you’ll see soon.

As of now, Wex Preview 1 Provides the following Triggers

  • EventTrigger – Will be fired when an event is raised. Can listen to events of Elements, or events from your view model
  • ReactiveTrigger – Can ‘import’ an Observable that you may ‘export’ using MEF. Useful to define and use custom events using System.Reactive.

And the following actions

  • InvokeMethodAction – Invoke a method directly in the View model or for a target element, supports passing parameters via Xaml
  • InvokeCommandAction – Invoke an ICommand in the view model
  • StoryBoardAction – Start, Stop, Pause, Resume, or Reverse story boards
  • PropertyAction – Sets a property value (very crude as of now)

Event Trigger

You can hook up the event trigger against your view model, or against an element. For example, assume you want to invoke a command in your view model based on various conditions – let us say, when the user presses the key ‘A’, and if and only if a check box is checked. Here we go


Reactive Trigger

The concept of reactive trigger is based on the System.Reactive (LINQ to Events) framework and Managed Extensibility Framework. Basically, you can create an event definition, and import that as an event for a control.

Your exported event definition should match the signature Func<object,IObservable<EventResult>>. You can use the ObservableExport attribute in Wex.Lib to mark your trigger as an exportable part. Also, the name you provide to the ExportName attribute will be later used in Xaml to ‘import’ this trigger.


Step 2 – In your application startup, call Compose method in WexPartComposer, and pass your catalogs

In this case I’m simply passing and assembly catalog with the current assembly, because I’ve my trigger as part of my host app. And I’ve this line in the App.xaml.cs constructor.


Step 3 – Just use the trigger in your Xaml

Here we go, you can import the exported trigger, and this will get fired when ever a key is pressed.


You might have guessed this, but you can create very customized event definitions using System.Reactive. For example, here is how to create an event definition if you want the trigger to fire only when the arrow keys are pressed.


You can read this article on Reactive Extensions, to understand more on this

Actions in Wex

You might have already noticed in the above examples, how to use the actions like InvokeCommandAction, StoryBoardAction etc. What is interesting is, Wex allows you to fire actions based on conditions (See the EventTrigger example where we are specifying the InvokingConditions for the actions.

I specifically want to comment on the InvokeMethodAction in Slex, that allows you to Invoke a method in view model directly. Here we go. Assume that you have an Add method in your view model, like this.


Now, this is how to invoke the add method, passing some parameters. Here, we pass the text in txt1 and txt2 as parameters.


A Quick Overview

Have a sneak peak at the actual implementation of Wex. If you examine the Trigger hierarchy, you’ll find that we’ve a WexTrigger abstract base class, from where other triggers are getting inherited.

  • WexTrigger - The base class for all Wex framework triggers
  • EventBasedTrigger - A base class for inheriting event based triggers
  • ObserverTrigger - An abstract class for creating triggers based on an Observable (System.Reactive)

WexTrigger directly inherits from System.Windows.Interactivity.TriggerBase<DependencyObject>. I’m not going to explain the System.Windows.Interactivity infrastructure and how each trigger is implemented, but you can definitely go through the source code and find the same yourself. Probably I’ll explain implementation details in future posts.




Now, let us have a quick look at the Actions involved. Have a look at the actions in the framework. WexTriggerAction is inherited from System.Windows.Interactivity.TriggerAction<FrameworkElement>, and from there on, you can go and explore as of now if you’ld like to :)


As I mentioned, Wex is a quick port of Slex, and seriously, there are lot of areas I still need to re-write to leverage what is already available in WPF (Especially on dependency property hooking and all). How ever, I thought it might be good if I bring out a first cut, so that you can also hack around with me, and leverage some of these concepts in your own apps.

And you may hit few bugs with this Preview, fix it yourself or wait for the Preview 2. But please give feedback!!

Enjoy Coding!!

Shout it
© 2012. All Rights Reserved. Amazedsaint.com