c# - 是否可以在 WPF 的 MultiBinding QuickConverter 中使用带有参数的经典转换器?
问题描述
是否可以在 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. '
我是在正确的方式还是不可能做到这一点?
感谢您的关注。
解决方案
当然可以。
如果要添加一个值作为 a System.Windows.Data.Binding
(或System.Windows.Data.MultiBinding
等)的结果,则必须使用P0
...P9
的属性之一QuickConverter.MultiBinding
,因为它们只接受绑定表达式。V0
...V9
属性只接受常量值,不接受绑定表达式。
<TextBlock Text="{qc:MultiBinding '$V0 + \' v\' + $P0',
V0={x:Static resx:Resources.String225},
P0={Binding Converter={StaticResource ProgramVersionConverter}, ConverterParameter='myParameter'}}"/>
推荐阅读
- c++ - 在命令提示符下使用 GCC 构建 WxWidgets 失败
- php - 使用 HttpClient 发送多个文件
- unix - 在卸载的挂载点下创建的文件
- r - 在 R 中使用 dplyr 和lazyeval 函数
- html - 法罗小话。html文件模板中的copyReplaceAll到静态页面文件
- c# - 如何将图像转换为 Base64 字符串?
- drupal - 如何在 Drupal 7 中预订页面后导航 URL
- jquery - 如何在本地实现 jQuery UI Touch Punch 和 Jquery-ui?
- c# - 拦截控制器
- java - Android Studio FirebaseRecyclerAdapter 只过滤一次