Tuesday, 19 November 2013

Wpf Behavior in a Style

Often when using the MVVM design pattern we find ourselves writing custom Behaviors. These are little bits of code that could be written in the code behind. However by creating a behavior we have a reusable bit of code that can be added inline with Xaml. There are various reasons you might create a custom behavior but one typical example is a lack of dependency properties on some control you are using.

Lets take a concrete example, imagine you wanted to know the selected rows in a grid control and this control didn't expose such a dependency property. Well you could write a custom behavior something like below:-


As you can see this exposes an attached property called SelectedRows. We can now bind our view model to this like below:-


Now imagine you want something like this for every instance of GridControl because you have a generic view model which will bind to this property. Well you'd use a style of course...hmmm

It turns out that Behaviors does not have an accessible setter, see style error below:-


So how do we get around this limitation?
Well we create an attached property that holds a collection of Behaviors like so:-


We can then create a style for this like below. Notice our list of Behaviors is defined as x:Shared=False so each instance of this style will get its own collection.


Tuesday, 12 November 2013

Common Code - Unity Extensions

One of the annoying things in Unity is you cannot find out if a class has been registered. Instead you must resolve the instance and catch an exception. This handy class will encapsulate that nonsense for you.


Common Code - Poco Messaging

When using PRISM you are given the option of using the EventAggregator for messaging. Whilst this is a useful construct I find it a little heavy weight and prefer a simple message bus with Pocos. Here is a guide to classes used in this simple messaging.







An Example Message




Common Code - UriStringSpeller - BitmapImage from String

Occasionally you will need to get a BitmapImage for toolbar or other UI controls. If these images are already embedded as a Resource (this is a build action in Visual Studio) you will need some way to specify the Uri for the BitmapImage. WPF has a fairly unusual looking notation for this so I created a helper class call UriStringSpeller. The class has two methods, one assumes the image is in the same assembly as the UriStringSpeller and the other allows you to specify an alternative assembly. I have a lot of common images so the first method is useful for those since they sit in the same assembly.


Common Code - NotifyPropertyChangedBase

When writing WPF applications and following the Mvvm pattern I often find myself using a lot of common code. I've decided to write a series of blog posts dedicated to those common code elements. This will be the first in that series.

INotifyPropertyChanged - A common interface on view model classes. I have created an abstract class called NotifyPropertyChangedBase that encapsulates this interface in a generic way. My view model classes then inherit from this. Lets jump to the code.




Dynamically update background and foreground of grid rows

I recently had a requirement to change the background and foreground colour of the rows in a WPF grid based on some criteria specified by the user. This involved showing a dialog following the Mvvm pattern ( I will blog a post on that in due course too). From this dialog the user could select one or more criteria which would be evaluated at runtime to affect the colouring of grid rows.

I'm using the DevExpress grids here but the solution is most likely applicable to many different vendors.

There's few different ways to achieve this but I chose to expose two properties called BackgroundBrush and ForegroundBrush on the row view model. I then use a style to keep the grid in synch with the row view model.