簡體   English   中英

是否可以在 WPF 的 MultiBinding QuickConverter 中使用帶有參數的經典轉換器?

[英]Is it possible to use classical converter with parameters in MultiBinding QuickConverter in WPF?

是否可以在 WPF 中使用帶有 QuickConverter MultiBinding 參數的經典轉換器?

更清楚地說,我想綁定 TextBlock 的 Text 屬性以顯示如下文本:

<MyApplication> + ' v' + <1.0>

MyApplication來自字符串資源Resources.String2251.0可能來自IValueConverter class 類型,我可以將參數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.MultiBindingP0 ... P9屬性之一,因為它們接受綁定表達式。 V0 ... V9屬性接受常量值,不接受綁定表達式。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM