![](/img/trans.png)
[英]Is it possible to use both a converter AND a StringFormat in WPF MultiBinding?
[英]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.String225
和1.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.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'}}"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.