I am working on an IValueConverter in Silverlight. This value converter needs to loop through a collection of MyOption elements and get a match. The MyOption values actually come from a database. I'm using this converter within a DataGrid. Because of that, i do not want to hit the database everytime. Rather, I want to hit the database once, and pass the options to the converter. To accomplish this, I thought I would expose a property and bind my collection of MyOption elements to it as shown here:
<converters:MyTypeConverter x:Key="myTypeConverter" UpdateTypes="{Binding Path=MyOptions}" />
...
<TextBlock Text="{Binding Path=OptionID, Converter={StaticResource myTypeConverter}}" />
I then define MyTypeConverter as shown here:
public class MyTypeConverter : UIElement, IValueConverter
{
public ObservableCollection<MyOption> Options
{
get { return (ObservableCollection<MyOption>)GetValue(OptionsProperty); }
set { SetValue(OptionsProperty, value); }
}
public static readonly DependencyProperty OptionsProperty =
DependencyProperty.Register("Options",
typeof(string), typeof(MyTypeConverter), null);
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string result = SomeObject.Convert(value, Options);
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
Unfortunately, I cannot seem to get this to work. It's almost like I can't bind to a converter. I get a compile-time error that says: "The type System.Windows.UIElement has no constructors defined." At the same time, I don't know how to pass in MyOptions to the type converter so that I'm not doing multiple round trips to the server.