I had also been recently looking at binding to dependency properties (Silverlight 4 Custom Property Binding), so it was something I had fresh in my mind. I knew that to register a DependencyProperty the owner needed to inherit from the DependencyProperty base class. So as well as implementing the usual IValueConverter interface, I made my class inherit from DependencyObject like so:
public class CustomValueConverter : DependencyObject, IValueConverter
I then declared a couple of dependency properties in my class; firstly a Dictionary<int, string> LookupDictionary and secondly a bool UpperCase , like so:
public static readonly DependencyProperty LookupDictionaryProperty = Dep endencyProperty.Register ( "LookupDictionary", typeof(Dictionary<int, str ing>), typeof(CustomValueConverte r), null ); public Dictionary<int, string> LookupDictionary { get { return (Dictionary<i nt, string>)GetValue( LookupDictionaryProperty); } set { SetValue( LookupDictionaryProperty, valu e); } } public static readonly Depende ncyProperty UpperCaseProperty = DependencyProperty.Register ( "UpperCase", typeof(bool), typeof(CustomValueConverte r), null ); public bool UpperCase { get { return (bool) GetValue(UpperCaseProperty); } set { SetValue( UpperCaseProperty, value); } }
The next thing I wanted to do was actually use the properties in my Convert method, so as an example I did something like this:
public object Convert(objectvalue, Type targetType, object parameter, CultureInfo culture) { string lookup = string. Empty; if (!(value == null || LookupDictionary == null)) { if (value is int) { if (UpperCase) lookup = LookupDictionary[(int)value]. ToUpper(); else lookup = LookupDictionary[(int)value]; } } return lookup; }
That's basically all you need to be able use your converter in your xaml, and set values for the properties we have declared in it.
Here is the entire example of the Value Converter class example:
using System; using System.Windows; using System.Windows.Data; using System.Globalization; using System.Collections.Generic; public class CustomValueConver ter : DependencyObject, IValue Converter { public static readonly Dep endencyProperty LookupDictionaryProperty = Dep endencyProperty.Register ( "LookupDictionary", typeof(Dictionary<int, string>), typeof(CustomValueConv erter), null ); public Dictionary<int, str ing> LookupDictionary { get { return (Dictiona ry<int, string>)GetValue( LookupDictionaryProperty); } set { SetValue( LookupDictionaryProperty, valu e); } } public static readonly Dep endencyProperty UpperCaseProperty = Dependency Property.Register ( "UpperCase", typeof(bool), typeof(CustomValueConv erter), null ); public bool UpperCase { get { return (bool) GetValue(UpperCaseProperty); } set { SetValue( UpperCaseProperty, value); } } public object Convert(obje ct value, Type targetType, obj ect parameter, CultureInfo culture) { string lookup = string .Empty; if (!(value == null || LookupDictionary == null)) { if (value is int) { if (UpperCase) lookup = LookupDictionary[(int)value]. ToUpper(); else lookup = LookupDictionary[(int)value]; } } return lookup; } public object ConvertBack( object value, Type targetType, object parameter, System. Globalization.CultureInfo culture) { throw new NotImplement edException(); } }}