Handling null values and missing object properties in Silverlight 4
- by PeterTweed
Before Silverlight 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.
Steps:
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()
{
InitializeComponent();
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:
<Grid.RowDefinitions>
<RowDefinition Height="60*" />
<RowDefinition Height="28*" />
<RowDefinition Height="28*" />
<RowDefinition Height="30*" />
<RowDefinition Height="154*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="86*" />
<ColumnDefinition Width="314*" />
</Grid.ColumnDefinitions>
<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!