有没有办法我可以做到这一点Style:
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控件本身发送到我的转换器类。
Tag
该ConverterParameter属性无法绑定,因为它不是依赖属性。
ConverterParameter
因为Binding不是从DependencyObject它的任何属性中派生的都不能是依赖属性。因此,一个 Binding 永远不能成为另一个 Binding 的目标对象。
Binding
DependencyObject
然而,还有一个替代解决方案。您可以将 aMultiBinding与多值转换器一起使用,而不是普通的 Binding:
MultiBinding
<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>
多值转换器获取源值数组作为输入:
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(); } }