I recently discovered that Silverlight 2 has the ICommand interface, but does not have ICommandSource. Basically, this means that you cannot easily execute a command when, say, a user clicks a button. This is pretty strange to me, and renders Silverlight’s ICommand interface next to worthless. If you’re an MVVM zealot like me, I’m sure you must see where I’m coming from here…

So, I corrected the situation. You can now execute commands, pass them parameters, and even honor their CanExecute status, all via some attached property magic. Read more about it, and download the source code, here.

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:

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…

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

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:

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.

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

I guess that I didn’t get enough geekery at the PDC, so I spent this weekend working on a new feature in Crack.NET. You can now open Reflector directly to any type or type member, simply by right-clicking on a Type in the TreeView or Member Name in the ListView, and then clicking on the ‘Open in Reflector’ menu item. The “Version 1.2 News” section on the Crack.NET homepage shows screenshots of this feature in action.

You can download the latest binaries here. If you need to run the app on a 64-bit version of Windows, you will need to download the source code, set the solution’s x64 compiler switch, and build it.

Walt Ritscher created a great tool that lets you easily work with WPF shader effects, called Shazzam. This tool makes it very easy to grab some shader code, perhaps off the internet, try out various input values, see what effect they have on images, and then grab some generated source code (C# or VB) and paste it into your app. Very cool!

Walt showed me this snazzy tool in action at PDC. He also made some videos that walk through how to put the tool to use. It is very easy to use, and makes it fun to play with shader effects. If you are interested in WPF shader effects, I suggest you check it out here: