1

Extending the following question:

Defining a Property in a IValueConverter class

My question is:

In the xaml file, TrueValue is set to a single value:

<CheckBox IsChecked="{Binding item, Converter={converter:ValueConverterWithProperties TrueValue=5}}"></CheckBox>

Is it possible to bind a property in a ValueConverter to some kind of List? How would the binding expression look like?

Joe M. Doe
  • 61
  • 6

2 Answers2

1

Binding to parameters or properties of IValueConverter does not work AFAIK, but you can use a IMultiValueConverter and just bind the additional values to the desired property:

<MultiBinding Converter="{StaticResource MultiValueConverter}">
    <Binding Path="YourValue" />
    <Binding Path="YourParameter" />
</MultiBinding>

and then use values[0] as the actual value and values[1] as the parameter.

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
  • Well, thanks, but that's not what I had in mind. If the TrueValue-Property is not of type bool, but of type List or IEnumerable . Is it possible to bind e.g. the ItemsSource of a ListBox to that property? – Joe M. Doe Jan 31 '18 at 10:40
  • 1
    @JoeM.Doe It would work the same way. The answer makes no assumption about the type of the Bindings source properties. `YourParameter` could well be an IEnumerable. – Clemens Jan 31 '18 at 10:43
  • An alternative may be a converter with a (bindable) dependency property, as described here: https://stackoverflow.com/q/6856321/1136211 – Clemens Jan 31 '18 at 10:44
1

You can declare a dependency property in the converter class, declare your converter as static resource and bind the property to a view model property.

This will work:

<Window x:Class="..."
        x:Name="_this" 
        ...>
<Window.Resources>
    <local:DepPropConverter x:Key="Convert"
        MyList="{Binding DataContext.YourListInViewmodel, Source={x:Reference _this}}"/>
</Window.Resources>
<CheckBox IsChecked="{Binding item, Converter={StaticResource Converter}}"></CheckBox>

The converter:

public class DepPropConverter : DependencyObject, IValueConverter
{
    public static readonly DependencyProperty MyListProperty =
        DependencyProperty.Register(
            nameof(MyList), typeof(IList), typeof(DepPropConverter));

    public IList MyList
    {
        get { return (IList)GetValue(MyListProperty); }
        set { SetValue(MyListProperty, value); }
    }

    public object Convert(
        object value, Type targetType, object parameter, CultureInfo culture)
    {
        //your logic here
        return value;
    }

    public object ConvertBack(
        object value, Type targetTypes, object parameter, CultureInfo culture)
    {
        //your logic here
        return value;
    }
}
Rekshino
  • 6,954
  • 2
  • 19
  • 44
  • Without Source={x:Reference _this} it doesn't work, at least by initialisation! – Rekshino Jan 31 '18 at 11:37
  • Thanks Rekshino, in a common UserControl/DataContext environment this works absolutely fine. I did see the x:Reference. My environment is a custom control as part of a ListBoxItem. Therefore, I can't set the x:Reference. I set a x:Type instead, but no data reaches the converter. – Joe M. Doe Jan 31 '18 at 14:17