Advanced MVVM now available on Kindle in Germany

April 21, 2011

Guten tag! If you happen to live in Germany and would like to read my ‘Advanced MVVM’ book on your Amazon Kindle device, you are in luck. Amazon just made my book available in Deutschland. Note: the book has not been translated to German.

You can grab a copy here:

By the way, folks in the UK can get Advanced MVVM on their Kindle here.

Learn more about Advanced MVVM here.

On a side note, I am shocked at how many copies of my book have sold. Over the past year, I’ve sold many thousands of copies and received a ton of great feedback, mostly positive. It has definitely been a great learning experience for me!


Article about Service Locator, MVVM, and MessageBoxes

April 1, 2010

I just published an article titled ‘Using a Service Locator to Work with MessageBoxes in an MVVM Application‘ on CodeProject.  It’s one solution to the now canonical question of how to work with message boxes in an MVVM app, either WPF or Silverlight.  The article gives an introduction to the Service Locator pattern, in case you are not yet familiar with that concept, and then dives into an example of how to leverage it.  The end result is a simple, testable, extensible way to work with message boxes from ViewModel objects.

By the way, I found out today that I got the Microsoft MVP award again.  Four years in a row!  Woohoo!! 😀

Two Model-View-ViewModel Screencasts

February 25, 2009

I have great news for anyone who is interested in learning how to create MVVM applications, either in WPF or Silverlight!  Craig Shoemaker, host of the Polymorphic Podcast and Pixel8 podcast, recently published two screencasts that show how to build MVVM applications from the ground up.  I am the guest on the WPF screencast, and Tim Heuer leads the way in the Silverlight version.  I think that the WPF screencast is a great complement to my ‘WPF Apps With The Model-View-ViewModel Design Pattern’ article.

Also, if you are interested in User Experience Design Patterns (which you should be if you are a professional front-end developer!), be sure to check out Craig’s podcast about Quince, with Ambrose Little.

Here are the links:

Implementing Model-View-ViewModel in WPF

Implementing Model-View-ViewModel in Silverlight

Implementing MVVM & Exploring UX Design Patterns


Creating an internationalized WPF Wizard

December 17, 2008

Karl Shifflett and I have been hard at work on a WPF Wizard application that is fully globalized and localized.  I just published an article about it on CodeProject, and am extremely happy with how it turned out.  If you’re interested in reading it, here’s the link:

The Philosophies of MVVM

December 1, 2008

There was a very long and interesting thread about MVVM today amongst the WPF Disciples.  The thread started out with a simple thought: using a ViewModel eliminates most scenarios where value converters are necessary.  My point was that a ViewModel class is essentially a value converter on steroids, thus rendering the IValueConverter interface irrelevant for most bindings.

This comment lead to a very engaging discussion about peoples’ fundamental philosophies regarding how a well-designed WPF application should be built.  Some people strongly disagreed with me, and gave lucid explanations of why.  It was a real eye-opener for me to have fellow MVVM advocates express opinions that, while completely valid and reasonable, are strikingly different from my own.

The thread even took a brief detour into something I’ve been thinking about for a while now but never publicly discussed: MMVVVVM!  Yeah, I know, that’s a terrible name for a design pattern…

If this sort of thing interests you, I highly suggest you set aside some time to read this thread.  What are your thoughts about this?

Using a ViewModel to Provide Meaningful Validation Error Messages

November 14, 2008

In my MSDN Magazine article about WPF data binding, I briefly mentioned how relying on the binding system to create input validation error messages does not usually lead to a very good user experience.  For example, suppose you bind the Text property of a TextBox to a property of type Int32, and the user types “foo” into the TextBox.  When the binding system tries to convert that string to an int, a FormatException is thrown and subsequently eaten by the data binding pipeline.  The standard technique of showing this validation error, as per most of the WPF documentation and examples, is to set ValidatesOnExceptions to true on the Binding assigned to the TextBox’s Text property.  This approach results in an error message like “Input string was not in a correct format” to be displayed to the user.  Unfortunately, most users have absolutely no idea what that cryptic message means.

The solution to this problem, as with so many others, is to use the Model-View-ViewModel (MVVM) pattern.  Since my article was about data binding, I did not have the liberty to show a better solution to this problem.  During a great conversation with my buddy Karl Shifflett the other night, this topic resurfaced, and I decided that it is time to show a better way.

Let’s take a step back and reassess the problem.  We have a property of a type that can be represented correctly by some, but not all, strings.  WPF does not currently ship with an editor control that knows how to work with this data type correctly (such as having a NumericUpDown control).  When the user types an invalid string into the TextBox that displays a textual representation of the property value, the WPF binding system swallows the parsing exception and, optionally, allows us to display the exception’s error message in the UI.  Most users do not understand what these error messages mean. We need a way to provide meaningful error messages, but to do that it seems that we must somehow crack open that black box known as the WPF data binding pipeline.  Since we cannot do that in a clean and simple way, we cannot take that approach.

The key to this problem is where the input value parsing occurs.  If we let the data binding pipeline parse the input text, we have no way of providing custom error messages, so we must handle the parsing ourselves.  In order to do that, we must expose a property of type String, not Int32, on a ViewModel object, and then bind the TextBox.Text property to that.  If we do that, the binding system has no need to parse the user’s input text for us, because a String is a String, and therefore no type conversion is required.  Once we have taken on the responsibility of parsing the input text, we suddenly have unfettered control over the error messages displayed in the UI.  This allows us to have a user-friendly editing experience, such as the one seen below:


In the demo app, which is available to download at the end of this post, there is a Model class called Person.  It is defined as:

Person class

The Person class implements “business rules” (to use the term loosely) by checking that the Age value is not set to an inappropriate value.  Note, the Age property of the Person class is of type Int32.  In order to make use of the technique I’m proposing, we must create a ViewModel class that wraps a Person instance.  Due to a severe lack of imagination, I named that class PersonViewModel…

PersonViewModel diagram

Within PersonViewModel is another Age property, but this time it is of type String:

PersonViewModel Age property

The TextBox in the Window has its Text property bound to this Age property, not the Age property on the Person object seen previously.  The user can type whatever they want into that TextBox, but the underlying Person object won’t have its Age updated unless they actually type in a valid whole number.  The first layer of defense against invalid input is in PersonViewModel’s implemention of IDataErrorInfo, as seen below:

PersonViewModel implements IDataErrorInfo

The second layer of defense against invalid input is in the Person class, which was listed in its entirety earlier in this post.  That validation logic is only invoked if the input text was able to be parsed to an integer.  As the screenshot below demonstrates, the Person object’s validation logic is still enforced.

Invalid Age message coming from Person object

Download the demo project here.  Note: be sure to change the file extension from .DOC to .ZIP and then decompress it.

Using MVVM to work with CheckBoxes in XamDataGrid

September 4, 2008

On my other blog, I published a way to put CheckBoxes into the XamDataGrid.  The interesting aspect of the implementation, in my opinion, is how it uses the Model-View-ViewModel pattern to maintain the check state of each CheckBox.  Since I put a CheckBox into the header area above the CheckBoxes in each row, I used a ViewModel object to encapsulate the logic that figures out what state the header CheckBox should have, and how to update the state of each row’s CheckBox.  It’s just another example of how great MVVM is when working with WPF.

Here’s a screenshot of that demo app:

You can read that blog post here.

Improvements made to my MVVM commanding solution

July 26, 2008

I woke up extra early this morning and just had to implement some enhancements to my recent MVVM commanding solution.  I took Bill‘s brilliant advice and made a concrete CommandSink class. That alone is a HUGE improvement!  I made a fix that allows the CommandSink attached property to work for elements in a template.  This is related to the timing issues Bill and I discussed, wherein there is no guarantee that my attached property will be invoked *after* an element’s command bindings are created and added to its CommandBindings property.  Also, I renamed RelayCommandBinding to CommandSinkBinding because I think that more accurately expresses the purpose of the class.

The article has been updated to explain the new features.  All of the important changes are in the How It Works section.  Also, the source code download was updated.  Here’s the link:

Article about using routed commands with MVVM

July 24, 2008

I just published an article on CodeProject that discusses a technique which simplifies the use of routed commands in an MVVM architecture.  If you’re interested, here is the link: