Here we are with the 4th article already in my Silverlight Advent Calendar series!
When using data binding in Silverlight, we often have to include one or more converters. A converter is a class that implements the IValueConverter interface. This class defines 2 methods, Convert and ConvertBack. Convert is applied when the data in the data binding action flows from source object to target control. A common use for converters is formatting a date value that comes from the database into a specific format or adding a currency symbol to a double value. Below is a sample converter class:
public class CurrencyConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{ double amount = double.Parse(value.ToString());
if (amount < 0)
return "- " + amount.ToString("c", culture); else
return amount.ToString("c", culture); }
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{ throw new NotImplementedException();
}
#endregion
}
Silverlight 4’s data binding engine has been extended with a few options that in some cases can avoid forcing us to create a converter. More specifically, three new options have been added: TargetNullValue, StringFormat and FallbackValue.
For these samples, we’ll use a class called Customer:
public class Customer
{ public string FirstName { get; set; } public string LastName { get; set; } public DateTime BirthDate { get; set; } public string Email { get; set; }}
I have instantiated this class and set it as DataContext for the entire UserControl:
customer = new Customer()
{ FirstName = "Gill",
LastName = "Cleeren",
BirthDay = new DateTime(1979, 1, 1),
Email = "gillcleeren@somewhere.com",
CustomerAddress = null
};
this.DataContext = customer;
Let’s take a look at what these new features offer us. TargetNullValue can be used in a data binding expression to specify what value should be used in case the binding returns null. In the previous sample, there is no value specified for the CustomerAddress property (it is null). If we want some replacement text, in this case “Unknown”, to be placed instead, we can use TargetNullValue like so:
<TextBlock x:Name="AddressTextBlock" Grid.Row="4" Grid.Column="0"
Text="Address:" FontWeight="Bold"></TextBlock>
<TextBlock x:Name="AddressValueTextBlock" Grid.Row="4" Grid.Column="1"
Text="{Binding CustomerAddress, TargetNullValue=Unknown}" ></TextBlock>
StringFormat can be used to format the value, just as was shown with the Converter earlier. For example, if we want the BirthDay to be formatted as a date in MM/dd/yyyy format, we can do so using the following code:
<TextBlock x:Name="BirthDateTextBlock" Grid.Row="2"
Grid.Column="0" Text="Birthday:" FontWeight="Bold"></TextBlock>
<TextBlock x:Name="BirthDateValueTextBlock" Grid.Row="2" Grid.Column="1"
Text="{Binding BirthDay, StringFormat=MM/dd/yyyy}"></TextBlock>
Finally, the FallbackValue can be used to display a value when the data-bound property on the source can not be located. In other words, this value will be displayed when we make an error in that we are trying to bind to a non-existent property. This is shown using the following code:
<TextBlock x:Name="InfoTextBlock" Grid.Row="5"
Grid.Column="0" Text="Info:" FontWeight="Bold"></TextBlock>
<TextBlock x:Name="InfoValueTextBlock" Grid.Row="5" Grid.Column="1"
Text="{Binding Info, FallbackValue=Nothing}" ></TextBlock>
The final result is shown below:
The complete code sample can be downloaded here: DatabindingInSL4.zip (64.44 KB)
Note: these new data binding features are a Silverlight 4 feature only!