【发布时间】:2020-09-18 10:39:46
【问题描述】:
是否可以在 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. '
我是正确的还是不可能做到的?
感谢您的关注。
【问题讨论】:
-
谢谢,但@neelesh bodgal,我正在使用QuickConverter 库github.com/JohannesMoersch/QuickConverter,我会用这个库来做。
标签: c# wpf parameter-passing converters multibinding