Using the ConverterParameter
The Binding object provides an additional property, which allows you to feed a parameter to the IValueConverter. This can be useful if you want to employ a converter in several related scenarios that are slightly different. Those familiar with formatting strings in .NET should be no stranger to the variety of FormatStrings available for built-in data types. The following example leverages the ConverterParameter to provide a FormatString.
The following example passes in the .NET short date format string '{0:d}' for display of the destination's start of peak season:
// Class for converting to a string based on the provided FormatString public class FormatStringConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
string formatString = (string)parameter; return String.Format(formatString, value);
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
throw new NotImplementedException();
<UserControl x:Class="Wrox.Silverlight30.Data.Convertion.Page"
xmlns="http://schemas.microsoft.com/winfx/200 6/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/200 6/xaml" xmlns:data="clr-namespace:Wrox.Silverlight3 0.Data.Convertion" Width="400" Height="3 00"> <UserControl.Resources>
<data:FormatStringConverter x:Key="FormatStringConverter" /> </UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White"> <StackPanel>
<TextBlock Text="{Binding PeakSeasonStart,
Converter={StaticResource FormatStringConverter}, ConverterParameter='{0:d}'}" /> </StackPanel> </Grid> </UserControl>
Post a comment