Reference links
https://stackoverflow.com/questions/534575/how-do-i-invert-booleantovisibilityconverter
Boolean reformer base class
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 reversal
For example, we sometimes have to show is false, true hidden
[ValueConversion(typeof(bool), typeof(Visibility))] public class BooleanToVisibilityConverter : BooleanConverter<Visibility> { public BooleanToVisibilityConverter() : base(Visibility.Visible, Visibility.Collapsed) { } }
Bool reversal
For example, we sometimes bind IsEnabled, but need to be the opposite of
[ValueConversion(typeof(bool), typeof(bool))] public class BooleanToInverseConverter : BooleanConverter<bool> { public BooleanToInverseConverter() : base(true, false) { } }