git_bsmd/ENI2/Util/InverseBooleanConverter.cs
2025-03-06 10:59:06 +01:00

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
}
}