git_bsmd/ENI2/Util/InverseBooleanConverter.cs

53 lines
1.5 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?))
throw new InvalidOperationException("The target must be a boolean");
if (!((bool?)value).HasValue) return null;
return !(bool?)value;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (targetType != typeof(bool?))
throw new InvalidOperationException("The target must be a boolean");
if (!((bool?)value).HasValue) return null;
return !(bool?)value;
}
#endregion
}
}