Why is IValueConverter ConverterParameter not Bindable?

41 Views Asked by At

This is not a duplicate. I am asking why the ConverterParameter property is not a BindableProperty by default. Is there a reason? The post that is supposedly a duplicate of this question outlines a workaround for this problem. I am not looking for a workaround, I am just curious as to why we even need a workaround in the first place.

I know that the IValueConverter ConverterParameter is not bindable. I also know how to work around this by using MultiBinding. Here, I am asking why ConverterParameter is not bindable (not necessarily looking for workarounds).

Consider this example:

public enum CoffeeType
{
    Latte,
    Cappuccino,
    Americano

}

public class Coffee
{
    public CoffeeType Type { get; set; }
}
//This converter returns the count of all the coffee orders of a given type
public class CoffeeOrdersCount : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is not List<Coffee> coffeList) return 0;
        if (parameter is not CoffeeType type) return 0;

        return coffeeList.Where(c => c.Type == type).Count;
    }

    ...ConvertBack...
}
<!-- This control allows the user to select a CoffeeType from a List -->
<SomeSelectControl
    x:Name="UserSelection"/>

<!-- This Label displays the total number of coffee orders for the selected type -->
<Label
    Text="{
        Binding CoffeeList,
        Converter={StaticResource CoffeeOrderCountConverter},
        ConverterParameter={x:Reference UserSelection.Value},
        StringFormat='{}There are {0} Coffee Orders of the Selected Type'"/>

It seems to me that this would be a valid (and useful) case for when one might need to bind to the ConverterParameter property. Am I misunderstanding how the IValueConverter is supposed to be used?

0

There are 0 best solutions below