Update 2.11.2009: I removed the DependencyObject and DependencyProperty references from the ViewModel. The solution works fine without them.
This is the start of a small mini series about how I would currently
approach the Notification Pattern in a WPF application, using the
default WPF practices and patterns. Think about it as a step by step
guide for using DataBinding, Control-Templates, Styles,
Resource-Inheritance, Converters and the Attached-Behavior-Pattern for
displaying Notifications in the WPF-UI. (I guess most of this is also
doable in Silverlight too, but I’ve never done anything with SL so I
can’t say for sure). I’ve also prepared a little bit of sample code for
this topic which will be downloadable soon.
First, some cautious warnings
I’m by no means a WPF-expert. I’m still in the learning phase. So, if there are more obvious or intuitive solutions to the problem, or I’m plain wrong about this, feel free to comment. I’m sure there is A LOT room for improvement.
What I’ve left out intentionally
I tried to strip everything unnecessary for the scope of
the post from the code. This includes for instance IoC, Convention over
Configuration, Code-Contracts, Expression-based Databinding setup and
some base class or extension refactorings. Don’t get me wrong, I
absolutely value those elements, but I wanted the example to be as easy
as possible while being somewhat useful for a ‘real world’ application.
The code might look a bit raw at at some edges because of this. So
please keep that in mind before throwing with stones ;-)
Some other prerequesites
A while back I wrote a post about implementing the Notification-pattern using
System.Componentmodel.DataAnnotations.
It demonstrates the implementation of a generic validator based on the DataAnnotation attributes. Besides that,
this
post about the Attached-Behavior-Pattern will also be useful for this
mini-series. Please give them a short visit if you haven’t read them
yet.
The example scenario
The scenario I’m going to use for the example is a very simple sign up form. It contains two input fields for username / email and a ‘sign-up’ button. It looks like this.

Rules associated with the fields are:
- Username is required and must at least be 5 characters long.
- Email is required and must contain a valid email address.
If any of the rules is broken, the related element should be visually highlighted …

… and when you hover over the element you’ll get a detailed description of the error from a tooltip.

I’m using the PresentationModel / MVVM pattern for the UI since it seems to be the default UI pattern in WPF.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
Did you notice it?
There is NO CODE in the View (Xaml and code behind) or the ViewModel which displays the Notifications.There’s also no base class for View or ViewModel where the code might be. What’s there on the other hand is only a collection of Notifications hanging of the ViewModel which is filled by the Validator. You might yourself now ask: Where is the damn glue code? Yeah I know dumb question because I answered it mostly in the introduction of the post. The interesting thing is that it isn’t a single location. As you will see the responsibilities for achieving the effect are separated between several components.
In the next posts we’re going to take a look at
- how to use the
Attached-Behavior-Patterntogether withStylesandresource based inheritancein order to match the Notification (s) from theViewModelto the relatedFrameworkElementsin the logical WPF tree. - how to use a
DataTriggeron anAttached Propertyin order to setup the Tooltip and showing the red border. - how to bind the Tooltip correctly to the related
Notification(s).
I hope I made you at least a bit curious …