WPF Boolean類型轉化器收集 反轉轉化器


參考鏈接

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

Boolean轉化器基類

public class BooleanConverter<T> : 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<T>.Default.Equals(flag, True);
    }
}

BooleanToVisibilityConverter反轉

比如我們有時候要false是顯示,true是隱藏

[ValueConversion(typeof(bool), typeof(Visibility))]
public class BooleanToVisibilityConverter : BooleanConverter<Visibility>
{
   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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM