Using the State Pattern for Adorning in XAML

Here’s a technique I used for adding overlay icons to represent various states of a base view model. Typically, you’ll have a DataTemplate for your primary view model. If the model can be in various states, however, it becomes annoying to manage the data triggers to change the look of the icon, e.g. using adorners.

This recipe can help take the implementation of representing these various states away from data triggers to another data template and a new data context object provided by a special converter. In doing so, it makes everything type safe and statically verifiable. It’s a variation of the State pattern where the behavior is rending a UI using data templates in XAML.

The state objects produced by the static converter are singletons thanks to a trick with generics… 🙂

Leave a Reply