Skip to main content

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

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

Popular posts from this blog

MVVM - Binding Multiple Radio Buttons To a single Enum Property in WPF

I had a property in my View Model, of an Enum type, and wanted to bind multiple radio buttons to this.

Firstly, I wrote a simple Enum to Bool converter, like this.

public class EnumToBoolConverter : IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (parameter.Equals(value)) return true; else return false; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return parameter; } #endregion }

And my enumeration is like

public enum CompanyTypes { Type1Comp, Type2Comp, Type3Comp } Now, in my XAML, I provided the enumeration as the ConverterParameter, of the Converter we wrote earlier, like

Creating a quick Todo listing app on Windows using IIS7, Node.js and Mongodb

As I mentioned in my last post, more and more organizations are leaning towards Web Oriented Architecture (WOA) which are highly scalable. If you were exploring cool, scalable options to build highly performing web applications, you know what Node.js is for.After following the recent post from Scott Hanselman, I was up and running quickly with Node.js. In this post, I’ll explain step by step how I’ve setup Node.js and Mongodb to create a simple Todo listing application.Setting up Node.jsThis is what I’ve done.1 – Goto, scroll down and download node.exe for Windows, and place it in your c:\node folder2 – Goto IIS Node project in Git at, download the correct ‘retail’ link of IIS Node zip file (I downloaded the already built retail package, otherwise you can download and build from the source).3 – Extract the zip file some where, and run the install.bat or install_iisexpress.bat depending on your IIS Version. If you don’t have IIS in…

Top 7 Coding Standards & Guideline Documents For C#/.NET Developers

Some time back, I collated a list of 7 Must Read, Free EBooks for .NET Developers, and a lot of people found it useful. So, I thought about putting together a list of Coding Standard guidelines/checklists for .NET /C# developers as well.As you may already know, it is easy to come up with a document - the key is in implementing these standards in your organization, through methods like internal trainings, Peer Reviews, Check in policies, Automated code review tools etc. You can have a look at FxCop and/or StyleCop for automating the review process to some extent, and can customize the rules based on your requirements.Anyway, here is a list of some good Coding Standard Documents. They are useful not just from a review perspective - going through these documents can definitely help you and me to iron out few hidden glitches we might have in the programming portion of our brain. So, here we go, the listing is not in any specific order.1 – IDesign C# Coding StandardsIDesign C# coding stand…