52 lines
1.5 KiB
C#
52 lines
1.5 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Globalization;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Windows;
|
|||
|
using System.Windows.Data;
|
|||
|
|
|||
|
namespace Plane.Windows.Converters
|
|||
|
{
|
|||
|
public class NullableBooleanConverter<T> : IValueConverter
|
|||
|
{
|
|||
|
public NullableBooleanConverter(T trueValue, T falseValue, T nullValue)
|
|||
|
{
|
|||
|
True = trueValue;
|
|||
|
False = falseValue;
|
|||
|
Null = nullValue;
|
|||
|
}
|
|||
|
|
|||
|
public T True { get; set; }
|
|||
|
public T False { get; set; }
|
|||
|
public T Null { get; set; }
|
|||
|
|
|||
|
public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|||
|
{
|
|||
|
if (value != null && !(value is bool?))
|
|||
|
throw new InvalidOperationException("The target must be a Nullable<bool>");
|
|||
|
var val = (bool?)value;
|
|||
|
return
|
|||
|
val == null
|
|||
|
? Null
|
|||
|
: val.Value
|
|||
|
? True
|
|||
|
: False;
|
|||
|
}
|
|||
|
|
|||
|
public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|||
|
{
|
|||
|
if (!(value is T))
|
|||
|
throw new InvalidOperationException($"The target must be a {typeof(T).Name}");
|
|||
|
var comparer = EqualityComparer<T>.Default;
|
|||
|
var val = (T)value;
|
|||
|
return
|
|||
|
comparer.Equals(val, True)
|
|||
|
? true
|
|||
|
: comparer.Equals(val, False)
|
|||
|
? false
|
|||
|
: (bool?)null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|