使用绑定作为ConverterParameter [英] Use Binding as ConverterParameter
问题描述
我正尝试使用值绑定作为转换器参数,如下面的代码片段所示:
I'm trying to use a value binding as converter parameter as shown in the code snippet below:
<Element
Attribute="{Binding Value,
Converter={StaticResource EqualityConverter},
ConverterParameter={Binding CompareTo}}" />
问题在于,调用EqualityConverter::Convert()
方法时将Binding
的实例作为转换器参数(CompareTo
),而不是绑定的具体值.
The problem is, that the EqualityConverter::Convert()
method is called with an instance of Binding
as converter parameter (CompareTo
) rather than the concrete value of the binding.
是否有更聪明的解决方法?我显然可以在视图模型中提供转换后的属性,但是我想知道是否有与上述解决方案相似的工作解决方案.
Is there a smarter way to solve it? I could obviously provide the converted property in my view model, but I would like to know if there is a similar working solution to the above one.
推荐答案
ConverterParameter不是依赖项属性,因此您将无法绑定任何属性.但是,您可以尝试将MultiBinding与MultiValueConverter结合使用.
ConverterParameter is not a Dependency Property, hence you won't be able to bind any property. However, you can try using MultiBinding with MultiValueConverter.
代码:
<TextBox>
<TextBox.Text>
<MultiBinding Converter="{StaticResource MultiValueConverter}">
<Binding Path="property1"/>
<Binding Path="property2"/>
</MultiBinding>
</TextBox.Text>
</TextBox>
public class MultiValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
property1 = values[0];
property2 = values[1];
// Do your logic with the properties here.
}
public object[] ConvertBack(object value, Type[] targetTypes,
object parameter, System.Globalization.CultureInfo culture)
{
}
}
这篇关于使用绑定作为ConverterParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!