Using Value Converters - Page 1
       by kirupa  |  29 July 2007

In the previous page, you tested our data binding and realized it didn't work. The solution that you embarked upon was creating a value converter. You created the C# class, but we haven't done anything more beyond that. Let's change that and add our value converter code.

Adding the Value Converter Code
Right now, in Visual Studio / C# Express, you will see the following code in our StringToBrush.cs file:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace KulerBackground
{
class StringToBrush
{
}
}

To make our StringToBrush class be a value converter, it needs to implement the IValueConverter interface. You can do that by using what looks like the syntax for extending a class:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace KulerBackground
{
class StringToBrush : IValueConverter
{
}
}

When you add the : IValueConverter text, you will see that you aren't in the clear yet. You have to specify which IValueConverter to use. When you right click on IValueConverter, the Resolve menu will appear, and from that menu, select System.Windows.Data:

[ resolve the ambiguity by using the System.Windows.Data namespace ]

After you have selected the System.Windows.Data namespace, you will see that you still have yet another thing to take care of - actually implementing the interface. Right click on your IValueConverter text and, from the menu that appears again, go to Implement Interface | Implement Interface:

[ implement the interface through the same context menu ]

Once you have selected the Implement Interface item from the above menu, the methods (and signatures) a class implementing this interface requires will appear in your code editor:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
 
namespace KulerBackground
{
class StringToBrush : IValueConverter
{
#region IValueConverter Members
 
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new Exception("The method or operation is not implemented.");
}
 
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new Exception("The method or operation is not implemented.");
}
 
#endregion
}
}

More specifically, using the IValueConverter interface requires your implementing class to have a Convert and ConvertBack method that takes the appropriate type and number of arguments. We'll primarily deal with the Convert method, so let's work on that for a bit.

Inside your Convert method, replace the throw new Exception line with the following code:

if (value != null)
{
string input = value.ToString();
 
if (input.Length != 6)
{
throw new Exception("String doesn't seem to be a valid RGB hex color");
}
 
Color newColor = (Color)ColorConverter.ConvertFromString("#" + input);
 
SolidColorBrush colorBrush = new SolidColorBrush(newColor);
return colorBrush;
}
else
{
return new SolidColorBrush(Colors.White);
}

After overwriting the exception line in your Convert method with the above code, be sure to resolve your SolidColorBrush namespace. When you build your project in Visual Studio / C# Express, you should not receive any errors. We are almost done, but there are a few things still left to do.

Don't worry if you do not fully understand what the above pasted code does. I will go through the code line-by-line towards the end of this tutorial so that you have a good understanding of why things work the way they do. In the meantime, let's move on to the next page and use our newly created value converter with our data binding.

Onwards to the next page!

1 | 2 | 3 | 4 | 5 | 6




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.