3

I have a ListBox where I set the ItemsSource to a CollectionViewSource that is populated from a WCF service. I use a value converter in the ListBox.ItemTemplate\DataTemplate to convert a property on the objects in the ItemsSource.

This all works as expected, but now I need to update the conversion process such that the converted value is still based on the individual object property values, except when a different property equals a specific value. And that value is set in the code behind of the xaml, this is field level variable set in the page constructor.

To do this I want pass the field level variable as the ValueConverter's ConverterParameter. I've tried several ways mainly using the ideas from this this question except I always get an error parsing the xaml (and the app fails to load).

One fix might be add a property to the objects in the ItemsSource but I don't really want to do that. Partially because that object is used in several projects so I don't want to change i .... and because I want to see if the other method is even possible. Also I believe wpf has a MultiValue Converter (and maybe SL 4.0) but I'm on SL 3.0.

My guess is the issues I've had so far are because of the timing of the loading, binding and the parsing of the xaml.

So a few questions:

  • can this be done as I describe above?
  • if not what is the issue?
  • if not is there another way without add a property to the object?
Community
  • 1
  • 1
Gern Blanston
  • 42,482
  • 19
  • 50
  • 64

1 Answers1

6

Doesn't sound to me like you want to be using the binding ConverterParameter in this case. If I've understood you correctly you have single value held at the UserControl level which you were wanting to assign to this parameter, therefore the parameter value for all instances would be the same.

Instead the approach I would take is to add a property to the Converter code itself.

 public class ExampleConverter : IValueConverter
 {

    public int SpecialValue { get; set; }

    public override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null)
        {
             if (value.Equals(SpecialValue))
                 return "Special!";
             else
                 return value.ToString();
        }
        else
        {
             return null;
        }
    }

    public override object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Now you'd have this converter added to the UserControl resources:-

<UserControl.Resources>
   <local:ExampleConverter x:Key="conv" SpecialValue="-1" />
</UserControl.Resources>

If you need to modify the value in the UserControl code you would use:-

((ExampleConverter)Resources["conv"]).SpecialValue = someOtherVariable;
AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • Thanks this works, I suspect some of my problems in my testing was the fact that I had been putting the Converter as a resource the main grid not in UserControl. – Gern Blanston Dec 09 '10 at 17:27