BililiveRecorder/BililiveRecorder.WPF/Converters/BoolToValueConverter.cs

34 lines
1.4 KiB
C#
Raw Normal View History

2020-11-27 18:51:02 +08:00
using System;
using System.Globalization;
using System.Windows;
2020-11-27 18:51:02 +08:00
using System.Windows.Data;
namespace BililiveRecorder.WPF.Converters
{
internal class BoolToValueConverter : DependencyObject, IValueConverter
2020-11-27 18:51:02 +08:00
{
public static readonly DependencyProperty TrueValueProperty = DependencyProperty.Register(nameof(TrueValue), typeof(object), typeof(BoolToValueConverter), new PropertyMetadata(null));
public static readonly DependencyProperty FalseValueProperty = DependencyProperty.Register(nameof(FalseValue), typeof(object), typeof(BoolToValueConverter), new PropertyMetadata(null));
2021-01-01 14:46:27 +08:00
public object TrueValue { get => this.GetValue(TrueValueProperty); set => this.SetValue(TrueValueProperty, value); }
public object FalseValue { get => this.GetValue(FalseValueProperty); set => this.SetValue(FalseValueProperty, value); }
2020-11-27 18:51:02 +08:00
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{
2021-01-01 14:46:27 +08:00
return this.FalseValue;
2020-11-27 18:51:02 +08:00
}
else
{
2021-01-01 14:46:27 +08:00
return (bool)value ? this.TrueValue : this.FalseValue;
2020-11-27 18:51:02 +08:00
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
2021-01-01 14:46:27 +08:00
return value != null ? value.Equals(this.TrueValue) : false;
2020-11-27 18:51:02 +08:00
}
}
}