62 lines
1.8 KiB
C#
62 lines
1.8 KiB
C#
// Copyright (c) 2017 schick Informatik
|
|
// Description: Bool'sche Werte für Bindings invertieren
|
|
//
|
|
|
|
using System;
|
|
using System.Windows.Data;
|
|
using System.Windows.Markup;
|
|
|
|
namespace ENI2.Util
|
|
{
|
|
|
|
[ValueConversion(typeof(bool?), typeof(bool?))]
|
|
public class InverseBooleanConverter : MarkupExtension, IValueConverter
|
|
{
|
|
|
|
private static InverseBooleanConverter _converter = null;
|
|
public override object ProvideValue(IServiceProvider serviceProvider)
|
|
{
|
|
if (_converter == null)
|
|
{
|
|
_converter = new InverseBooleanConverter();
|
|
}
|
|
return _converter;
|
|
}
|
|
|
|
#region IValueConverter Members
|
|
|
|
public object Convert(object value, Type targetType, object parameter,
|
|
System.Globalization.CultureInfo culture)
|
|
{
|
|
|
|
if (targetType == typeof(bool?))
|
|
{
|
|
if (!((bool?)value).HasValue) return null;
|
|
return !(bool?)value;
|
|
}
|
|
|
|
if (targetType == typeof(bool))
|
|
return !(bool) value;
|
|
|
|
throw new InvalidOperationException("The target must be a boolean");
|
|
}
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter,
|
|
System.Globalization.CultureInfo culture)
|
|
{
|
|
if (targetType == typeof(bool?))
|
|
{
|
|
if (!((bool?)value).HasValue) return null;
|
|
return !(bool?)value;
|
|
}
|
|
|
|
if(targetType == typeof(bool))
|
|
return !(bool)value;
|
|
|
|
throw new InvalidOperationException("The target must be a boolean");
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|