VPet/VPet.Solution/Converters/ValueToBoolConverter.cs

81 lines
1.9 KiB
C#
Raw Normal View History

2024-03-30 07:00:02 +00:00
using System.ComponentModel;
2023-12-30 15:50:37 +00:00
using System.Globalization;
2024-01-09 15:22:25 +00:00
using System.Windows;
2023-12-30 15:50:37 +00:00
namespace HKW.WPF.Converters;
public class ValueToBoolConverter : ValueConverterBase
{
2024-01-09 15:22:25 +00:00
/// <summary>
///
/// </summary>
public static readonly DependencyProperty TargetValueProperty = DependencyProperty.Register(
nameof(TargetValue),
typeof(object),
typeof(ValueToBoolConverter),
new PropertyMetadata(null)
);
/// <summary>
/// 目标值
/// </summary>
[DefaultValue(true)]
public object TargetValue
{
get => (object)GetValue(TargetValueProperty);
set => SetValue(TargetValueProperty, value);
}
/// <summary>
///
/// </summary>
public static readonly DependencyProperty InvertProperty = DependencyProperty.Register(
nameof(Invert),
typeof(bool),
typeof(ValueToBoolConverter),
new PropertyMetadata(false)
);
/// <summary>
/// 颠倒
/// </summary>
[DefaultValue(false)]
public bool Invert
{
get => (bool)GetValue(InvertProperty);
set => SetValue(InvertProperty, value);
}
/// <summary>
///
/// </summary>
public static readonly DependencyProperty NullValueProperty = DependencyProperty.Register(
nameof(NullValue),
typeof(bool),
typeof(ValueToBoolConverter),
new PropertyMetadata(false)
);
/// <summary>
/// 为空值时布尔值
/// </summary>
[DefaultValue(false)]
public bool NullValue
{
get => (bool)GetValue(NullValueProperty);
set => SetValue(NullValueProperty, value);
}
2023-12-30 15:50:37 +00:00
public override object Convert(
object value,
Type targetType,
object parameter,
CultureInfo culture
)
{
2024-01-09 15:22:25 +00:00
if (value is null)
return NullValue;
return value.Equals(TargetValue) ^ Invert;
2023-12-30 15:50:37 +00:00
}
}