In my last post we saw, that though it is possible to bind to a bound property, some implicit type conversions might occur. We can however create a custom type converter and bring a little more predictability into the way the type conversions happen.
We start by creating a class that implements the IValueConverter interface (found in System.Windows.Data). As part of the implementation we define two methods Convert – which is responsible for converting the source data type to destination data type and ConvertBack which is its antithesis. In the context of our example from the last post, Convert will take a SolidColorBrush and give us a String (the proper name of a color rather than its hex value). ConvertBack will do the reverse – the implementation being left as an exercise for the reader
.
Here is what the code looks like:
using System;
using System.Windows.Media;
using System.Windows.Data;
using System.Globalization;
namespace TypeConversion
{
class ColorToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (null != value)
{
System.Windows.Media.Color MColor;
MColor = ((SolidColorBrush)value).Color;
switch(MColor.ToString().ToLower())
{
case “#ffff0000″:
return “Red”;
case “#ff00ff00″:
return “Lime”;
case “#ff008000″:
return “Green”;
case “#ff0000ff”:
return “Blue”;
default:
return MColor.ToString();
}
}
else
{
return “bad color”;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
}
The code for conversion is a little inelegant for my taste. I wish there was a better way of doing this. I tried converting System.Windows.Media.Color to System.Drawing.Color and then using System.Drawing.Color.ToKnownColor(). Sadly, that works only if System.Drawing.Color was initialized using a System.Drawing.KnownColor enumeration member in the first place. One could build a look-up table to find the English name of a color given its Hex value but I didn’t bother.
Now in order to use this class we need to make some modifications to our XAML code:
<Page xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
xmlns:local=”clr-namespace:TypeConversion”
HorizontalAlignment=”Center”
VerticalAlignment=”Center”>
<Page.Resources>
<Local.ColorToStringConverter x:Key=”TheConverter”>
</Page.Resources>
<StackPanel Width=”400″>
<TextBox x:Name=”txtColor” BorderBrush=”Black” Text=”Type a color” Margin=”2,2,2,2″/>
<Rectangle x:Name=”rectColor” Height=”100″ Fill=”{Binding ElementName=txtColor, Path=Text, Mode=OneWay}” Margin=”2,2,2,2″ Stroke=”Silver”/>
<TextBox x:Name=”txtTextColor” BorderBrush=”Black” Text=”{Binding ElementName=rectColor, Path=Fill, Mode=OneWay, Converter={StaticResource TheConverter}}” Margin=”2,2,2,2″/>
</StackPanel>
</Page>
Basically, we first instantiate our type converter class as a static resource and then refer to it as part of the binding expression. You’ll also need to compile this XAML and the converter class into either an XBAP or a windows application. I have uploaded the pre-built XBAP which you can run now (IE only) or download the Visual Studio project and play with it.

