using System.ComponentModel; using System.Globalization; using System.Windows; namespace HKW.WPF.Converters; public class ValueToBoolConverter : ValueConverterBase { /// /// /// public static readonly DependencyProperty TargetValueProperty = DependencyProperty.Register( nameof(TargetValue), typeof(object), typeof(ValueToBoolConverter), new PropertyMetadata(null) ); /// /// 目标值 /// [DefaultValue(true)] public object TargetValue { get => (object)GetValue(TargetValueProperty); set => SetValue(TargetValueProperty, value); } /// /// /// public static readonly DependencyProperty InvertProperty = DependencyProperty.Register( nameof(Invert), typeof(bool), typeof(ValueToBoolConverter), new PropertyMetadata(false) ); /// /// 颠倒 /// [DefaultValue(false)] public bool Invert { get => (bool)GetValue(InvertProperty); set => SetValue(InvertProperty, value); } /// /// /// public static readonly DependencyProperty NullValueProperty = DependencyProperty.Register( nameof(NullValue), typeof(bool), typeof(ValueToBoolConverter), new PropertyMetadata(false) ); /// /// 为空值时布尔值 /// [DefaultValue(false)] public bool NullValue { get => (bool)GetValue(NullValueProperty); set => SetValue(NullValueProperty, value); } public override object Convert( object value, Type targetType, object parameter, CultureInfo culture ) { if (value is null) return NullValue; return value.Equals(TargetValue) ^ Invert; } }