Search code examples
c#wpfparameter-passingconvertersmultibinding

Is it possible to use classical converter with parameters in MultiBinding QuickConverter in WPF?


Is it possible to use classical converter with parameters with QuickConverter MultiBinding in WPF ?

More clearly, I want to bind the Text property of a TextBlock to display a text like this :

<MyApplication> + ' v' + <1.0>

MyApplication comes from a string resource Resources.String225 and 1.0 might comes from an IValueConverter class type to which I may pass the parameter myParameter . I tried the XAML code below,

<TextBlock Text="{qc:MultiBinding '$V0 + \' v\' + $V1',
 V0={x:Static resx:Resources.String225},
 V1={Binding Converter={StaticResource ProgramVersionConverter}, ConverterParameter='myParameter'}}"/>

With the following Converter :

public class ProgramVersionConverter : IValueConverter
{
    public static Func<string, string> GetApplicationExeVersion;

    /// <summary>
    /// Returns version of the executable
    /// </summary>
    /// <param name="value"></param>
    /// <param name="targetType"></param>
    /// <param name="parameter"></param>
    /// <param name="culture"></param>
    /// <returns></returns>
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return GetApplicationExeVersion?.Invoke((string)parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException("ProgramVersion converter ConvertBack not supported.");
    }
}

GetApplicationExeVersion is set to a method in another part of code, not needed there.

But I'm getting this Exception at runtime :

System.Windows.Markup.XamlParseException:
'Unable to set' Binding 'on property' V1 'of type' MultiBinding '.
A 'Binding' can only be defined on a DependencyProperty of a DependencyObject. '

Am I in the rigth way or it is not possible to do it ?

Thank you for your attention.


Solution

  • Of course, you can.
    If you want to add a value as a result of a System.Windows.Data.Binding (or System.Windows.Data.MultiBinding etc.), you have to use one of the P0...P9 properties of the QuickConverter.MultiBinding, as they only accept binding expressions. The V0...V9 properties only accept constant values, no binding expressions.

    <TextBlock Text="{qc:MultiBinding '$V0 + \' v\' + $P0',
     V0={x:Static resx:Resources.String225},
     P0={Binding Converter={StaticResource ProgramVersionConverter}, ConverterParameter='myParameter'}}"/>