25.April.08 – 8:38
WPF offers great functionality when it comes to DataBinding. You can e.g. set a List of Customer-Objects as DataContext on a ListView and don’t have to care about the rest.The following example shows a List of Customer’s bound to a ListView:

But what if you want to display some Properties of your objects in another format (i.e. something other that ToString() returns) ? You can either override ToString() or try to use another (imho more elegant) way.
Therefore WPF offers an Interface called IValueConverter
public interface IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture);
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
}
When implementing this interface in your own class, you can probably do everything you can imagine with the values you try to format. You can e.g. display the birthdate instead of its (normal) DateTime representation as Base64-string (this has no real use, it’s just an example of how far you can go). Therefore we create the following converter:
public class Base64Converter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return System.Convert.ToBase64String(Encoding.ASCII.GetBytes(value.ToString()));
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Encoding.ASCII.GetString(System.Convert.FromBase64String(value.ToString()));
}
#endregion
}
Next we have to add our Namespace to the Window:
<Window x:Class="CustomValueConverter:Window1"
. . .
xmlns:converters="clr-namespace:CustomValueConverter.Converter">
In the next step we create our Converter in our Window’s Resources:
. . .
<Window.Resources>
<converter:Base64Converter x:Key="base64"/>
</Window.Resources>
. . .
And from now on we can use our Converter everywhere we use DataBinding. For example:
. . .
<TextBlock Text="{Binding Path=Birthday,Converter={StaticResource base64}}"/>
. . .
would give the following result:

You can also pass parameters directly from XAML to a class implementing IValueConverter. This could be useful if you want to e.g. display some DateTime values in your own format.
Therefore we change our Convert()-method in the following way:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((DateTime)value).ToString((string)parameter);
}
Now the DateTime value will be formatted corresponding to our format string we supply as a parameter from XAML. We can declare our parameters like this :
<TextBlock Text="{Binding Path=Birthday,
Converter={StaticResource parameterized},
ConverterParameter='dd@MM@yyyy'}"/>
what gives us the following result

With ValueConverters you can transform your objects to any representation you want. You’re only limited by your imagination.
The ConvertBack() method of IValueConverter is needed to convert the values from Convert() back to its original representation.
This is necessary especially if you bind some values to TextBoxes (or other value-changing controls) and don’t want your user to use the original representation of your object’s values.
Posted in .NET, WPF, c#, development | 1 Comment »