参考链接
https://*.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) { } }