1

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.

JavaScript Developer
  • 3,968
  • 11
  • 41
  • 46

1 Answers1

0

This is where converter parameters come in. You need to send your options along with your OptionID:

<converters:MyTypeConverter x:Key="myTypeConverter" />
...
<TextBlock Text="{Binding Path=OptionID, 
                  Converter={StaticResource myTypeConverter}, 
                  ConverterParameter={Binding Path=MyOptions}}" />

TypeConverter:

public class MyTypeConverter : UIElement, IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
     var Options = parameter As ObservableCollection<MyOption>
     string result = SomeObject.Convert(value, Options);
     return result;
  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  {
     return value;
  }
}

You'll want to check that the parameter is not null and that is contains something after you pass it, but you get the point...

Barry Franklin
  • 1,781
  • 1
  • 27
  • 45
  • i think this won't work as you can't pass anything but a literal into the ConverterParameter. see this - http://stackoverflow.com/questions/1345170/pass-value-of-a-field-to-silverlight-converterparameter – Xin Jun 24 '13 at 14:41