是否可以在WPF的MultiBinding QuickConverter中使用带参数的经典转换器?

问题描述 投票:1回答:1

是否可以在WPF中将带有参数的经典转换器与QuickConverter MultiBinding一起使用?

更清楚地说,我想绑定TextBlock的Text属性以显示这样的文本:

<MyApplication> + ' v' + <1.0>

MyApplication来自字符串资源Resources.String2251.0可能来自IValueConverter类类型,我可以将参数myParameter传递给该类类型。我在下面尝试了XAML代码,

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

使用以下转换器:

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被设置为代码另一部分中的方法,在那儿不需要。

但是我在运行时遇到了这个异常:

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. '

我是严格的方式还是无法做到?

谢谢您的关注。

c# wpf parameter-passing converters multibinding
1个回答
0
投票

当然可以。如果要由于System.Windows.Data.Binding(或System.Windows.Data.MultiBinding等)而添加一个值,则必须使用QuickConverter.MultiBinding的P0 ... P9属性之一。 V0 ... V9属性仅接受常量值,不包含绑定表达式。

<TextBlock Text="{qc:MultiBinding '$V0 + \' v\' + $P0',
 V0={x:Static resx:Resources.String225},
 P0={Binding Converter={StaticResource ProgramVersionConverter}, ConverterParameter='myParameter'}}"/>
© www.soinside.com 2019 - 2024. All rights reserved.