是否可以在WPF中将带有参数的经典转换器与QuickConverter MultiBinding一起使用?
更清楚地说,我想绑定TextBlock的Text属性以显示这样的文本:
<MyApplication> + ' v' + <1.0>
MyApplication
来自字符串资源Resources.String225
,1.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. '
我是严格的方式还是无法做到?
谢谢您的关注。
当然可以。如果要由于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'}}"/>