【问题标题】:Is it possible to use classical converter with parameters in MultiBinding QuickConverter in WPF?是否可以在 WPF 的 MultiBinding QuickConverter 中使用带有参数的经典转换器?
【发布时间】:2020-09-18 10:39:46
【问题描述】:

是否可以在 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】:

当然可以。
如果要添加一个值作为System.Windows.Data.Binding(或System.Windows.Data.MultiBinding 等)的结果,则必须使用QuickConverter.MultiBindingP0...P9 属性之一,因为它们 仅接受绑定表达式。 V0...V9 属性接受常量值,不接受绑定表达式。

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

【讨论】:

    猜你喜欢
    • 2013-07-03
    • 2013-03-23
    • 2013-06-02
    • 2011-10-10
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    相关资源
    最近更新 更多