使用自定义类设置控件的可见性

时间:2011-09-15 07:16:52

标签: c# wpf xaml

<TextBlock.Visibility>
    <mat:MatcherConverter>
        <mat:Matcher MatchVisibility="Visible" DismatchVisibility="Collapsed">
            <mat:Matcher Value1="{Binding Boolean1}" Value2="True" ComparisonOperator="AND"/>
            <mat:Matcher Value1="{Binding Boolean2}" Value2="True" ComparisonOperator="AND"/>
        </mat:Matcher>
    </mat:MatcherConverter>
</TextBlock.Visibility>

您如何看待将MarkupExtension作为基类的类? 在这种情况下,它将是MatcherConverter。这个类将以递归方式遍历所有匹配器,结果是布尔值。

1 个答案:

答案 0 :(得分:0)

您可以在TheBooleanResult中创建一个属性Matcher,而不是返回您要评估的布尔结果(在ChildrenValue1Value2,... )。然后创建一个IValueConverter,其Matcher并获取TheBooleanResult并返回您想要的可见性。

public class MatcherConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Matcher m = (Matcher)value;
        return m.TheBooleanResult ? Visibility.Visible : Visibility.Hidden;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
相关问题