绑定转换器参数 [英] Binding ConverterParameter
问题描述
有没有一种方法可以在 Style
中做到这一点:
Is there a way I could do this in a Style
:
<Style TargetType="FrameworkElement">
<Setter Property="Visibility">
<Setter.Value>
<Binding Path="Tag"
RelativeSource="{RelativeSource AncestorType=UserControl}"
Converter="{StaticResource AccessLevelToVisibilityConverter}"
ConverterParameter="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag}" />
</Setter.Value>
</Setter>
</Style>
我只需要将顶级父级的 Tag
和控件本身的 Tag
发送到我的转换器类.
I simply need to send the Tag
of top-level parent and the Tag
of control itself to my converter class.
推荐答案
无法绑定 ConverterParameter
属性,因为它不是依赖属性.
The ConverterParameter
property can not be bound because it is not a dependency property.
因为 Binding
不是从 DependencyObject
派生的,所以它的任何属性都不能是依赖属性.因此,一个 Binding 永远不能成为另一个 Binding 的目标对象.
Since Binding
is not derived from DependencyObject
none of its properties can be dependency properties. As a consequence, a Binding can never be the target object of another Binding.
然而,有一个替代解决方案.您可以使用 MultiBinding
使用 多值转换器 而不是普通的绑定:
There is however an alternative solution. You could use a MultiBinding
with a multi-value converter instead of a normal Binding:
<Style TargetType="FrameworkElement">
<Setter Property="Visibility">
<Setter.Value>
<MultiBinding Converter="{StaticResource AccessLevelToVisibilityConverter}">
<Binding Path="Tag" RelativeSource="{RelativeSource Mode=FindAncestor,
AncestorType=UserControl}"/>
<Binding Path="Tag" RelativeSource="{RelativeSource Mode=Self}"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
多值转换器获取源值数组作为输入:
The multi-value converter gets an array of source values as input:
public class AccessLevelToVisibilityConverter : IMultiValueConverter
{
public object Convert(
object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values.All(v => (v is bool && (bool)v))
? Visibility.Visible
: Visibility.Hidden;
}
public object[] ConvertBack(
object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
这篇关于绑定转换器参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!