WPF Boolean类型转化器收集 反转转化器

参考链接

https://stackoverflow.com/questions/534575/how-do-i-invert-booleantovisibilityconverter

Boolean转化器基类

public class BooleanConverter : IValueConverter
{
    protected BooleanConverter(T tValue, T fValue)
    {
        True = tValue;
        False = fValue;
    }

    public T True { get; set; }

    public T False { get; set; }


    public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value is bool flag && flag ? True : False;
    }

    public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value is T flag && EqualityComparer.Default.Equals(flag, True);
    }
}

BooleanToVisibilityConverter反转

比如我们有时候要false是显示,true是隐藏

[ValueConversion(typeof(bool), typeof(Visibility))]
public class BooleanToVisibilityConverter : BooleanConverter
{
   public BooleanToVisibilityConverter() : base(Visibility.Visible, Visibility.Collapsed) { }
}

Bool反转

比如我们有时候绑定IsEnabled,但需要反着来

[ValueConversion(typeof(bool), typeof(bool))]
public class BooleanToInverseConverter : BooleanConverter<bool>
{
    public BooleanToInverseConverter() : base(true, false) { }
}

白嫖链接

https://github.com/yinghualuowu/SakuraStyle

你可能感兴趣的:(WPF Boolean类型转化器收集 反转转化器)