Looking for something else?
Dealing with null values in data sources can be tricky. Often when you get a null value from your source you want
to show an alternate output. For example, rather than showing a null string (which would result in an empty text control) you might want to show a more friendly value like "Not Available". Fortunately for us WPF has built in support for binding to null data sources.
Define the data source
First, let's take a look what happens when you have a text box bound to a null value. I'm going to be using the following Employee class as my data source.
As you can see there are a couple of string properties, one integer property and a few Nullable<T> properties defined in the employee class. I will instantiate the type and set a few properties as follows.
The following sample shows a typical XAML binding.
Since the LastName property is null the binding engine uses a fall back value when filling the Textbox. As a result the user sees an empty input control.
The TargetNullValue property
We can provide an alternate value to show whenever the binding source is null. All it takes is to set the TargetNullValue property in your binding.
Now, when that application is running the user will see the replacement text in the TextBox.
Working with value types
In .NET it's not possible to store a null value in a value type. If you provide a TargetNullValue for a control that's bound to value type, the alternate value is ignored.
Working with Nullable<T> types
There is a type in.NET that supports value types and nulls. Of course I'm talking about the generic Nullable<T> type. If you look at the Employee class again you'll see that the IsActive, CustomerSince, and LastOrderDate properties are all Nullable. Let's start by binding to the IsActive property.
If I bind a checkbox IsChecked property to the IsActive property and the underlying value is null the checkbox shows its indeterminate state.
The first checkbox in the following screenshot shows an indeterminate value.
If I use the TargetNullValue however I can remap it to the un-checked state.
Get current date
The next example is for a Nullable<DateTime >value. When the underlying value is null I want to show the current date. Notice how I'm using the x:Static markup extension to access the DateTime.Now property in the second date picker control.
Custom value converter
In this last example I'll look at something slightly more complex. Imagine for a moment that we have a library that retrieves images from the Internet. The Image control expects a URI for its source property so let's make our library function return a URI. Occasionally the library has trouble getting a valid URI for an image so in that case we want the binding to use a default image.
The key to this solution is to create a custom value converter. Under normal circumstances the value converter will return a URI. When our library cannot return a valid URI then it will return a null value.
Now we can use the TargetNullValue property to specify the default image.
Here's an example of our custom convert method.
Notice that I examine the parameter to determine what image to get. In this simple example I look for the 'blue' string and return a URI for a local image. If for some reason I can't retrieve the correct URI I returned null.
Now, in the XAML I'll create the default Image and converter and stored them in a resource dictionary.
Then it is simply a matter of adding the converter and the TargetNullValue property to my binding to complete the example.
In the first image element I'm providing 'brown' as my converter parameter. Consequently my converter returns null which results in the default image being shown. In the screenshot below the default is a red butterfly.