Geeks With Blogs
Peter Tweed Exploring and explaining the mysteries of .NET

Before 4 to bind a data object to the UI and display a message associated with either a null value or if the binding path was wrong, you would need to write a Converter. 

In Silverlight 4 we find the addition of the markup extensions TargetNullValue and FallbackValue that allows us to display a value when a null value is found in the bound to property and display a value when the property being bound to is not found.

This post will show you how to use both markup extensions.


1. Create a new Silverlight 4 application

2. In the body of the MainPage.xaml.cs file replace the MainPage class with the following code:

    public partial class MainPage : UserControl


        public MainPage()



            this.Loaded += new RoutedEventHandler(MainPage_Loaded);



        void MainPage_Loaded(object sender, RoutedEventArgs e)


            person p = new person() { NameValue = "Peter Tweed" };

            this.DataContext = p;




    public class person


        public string NameValue { get; set; }

        public string TitleValue { get; set; }


This code defines a class called person with two properties.  A new instance of the class is created, only defining the value for one of the properties and bound to the DataContext of the page.

3.  In the MainPage.xaml file copy the following XAML into the LayoutRoot grid:


            <RowDefinition Height="60*" />

            <RowDefinition Height="28*" />

            <RowDefinition Height="28*" />

            <RowDefinition Height="30*" />

            <RowDefinition Height="154*" />



            <ColumnDefinition Width="86*" />

            <ColumnDefinition Width="314*" />


        <TextBlock Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="32,0,0,0" Name="textBlock1" Text="Name Value:" VerticalAlignment="Top" />

        <TextBlock Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="32,0,0,0" Name="textBlock2" Text="Title Value:" VerticalAlignment="Top" />

        <TextBlock Grid.Row="3" Height="23" HorizontalAlignment="Left" Margin="32,0,0,0" Name="textBlock3" Text="Non Existant Value:" VerticalAlignment="Top" />

        <TextBlock Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Name="textBlock4" Text="{Binding NameValue, TargetNullValue='No Name!!!!!!!'}" VerticalAlignment="Top" Margin="6,0,0,0" />

        <TextBlock Grid.Column="1" Grid.Row="2" Height="23" HorizontalAlignment="Left" Name="textBlock5" Text="{Binding TitleValue, TargetNullValue='No Title!!!!!!!'}" VerticalAlignment="Top" Margin="6,0,0,0" />

        <TextBlock Grid.Column="1" Grid.Row="3" Height="23" HorizontalAlignment="Left" Margin="6,0,0,0" Name="textBlock6" Text="{Binding AgeValue, FallbackValue='No such property!'}" VerticalAlignment="Top" />


 This XAML defines three textblocks – two of which use the TargetNull and one that uses the FallbackValue markup extensions. 

4. Run the application and see the person name displayed as defined for the person object, the expected string displayed for the TargetNullValue when no value exists for the boudn property and the expected string displayed for the FallbackValue when the property bound to is not found on the bound object.

It's that easy!


Posted on Monday, April 26, 2010 3:35 PM Silverlight | Back to top

Comments on this post: Handling null values and missing object properties in Silverlight 4

# re: Handling null values and missing object properties in Silverlight 4
Requesting Gravatar...
Thanks, that was helpful.
Left by Michael on Jul 30, 2010 3:52 PM

# re: Handling null values and missing object properties in Silverlight 4
Requesting Gravatar...
I have tried using that markup extension but its not available - I am using Silverlight 4???
Left by Greg on Jul 22, 2011 8:13 AM

# re: Handling null values and missing object properties in Silverlight 4
Requesting Gravatar...
Hi ,

Is there is a way to provide a column name in "TargetNullValue" rather a string??? (Like TargetNullValue = LastNameValue)

Binding NameValue, TargetNullValue='No Name!!!!!!!'
Left by vasanth on Apr 17, 2012 2:40 PM

Your comment:
 (will show your gravatar)

Copyright © PeterTweed | Powered by: