using System; using System.Globalization; using System.Windows; using System.Windows.Data; using System.Windows.Markup; namespace AIDiagnosisDemo.Extensions { [MarkupExtensionReturnType(typeof(IValueConverter))] public class BoolToVisibilityConverterExtension : MarkupExtension { [ThreadStatic] private static BoolToVisibilityConverter _converter; public override object ProvideValue(IServiceProvider serviceProvider) { return _converter ?? (_converter = new BoolToVisibilityConverter()); } private class BoolToVisibilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var converterParameter = parameter?.ToString(); if (value == null) { return converterParameter == "Reverse" ? Visibility.Collapsed : Visibility.Visible; } if ((bool)value) { return converterParameter == "Reverse" ? Visibility.Collapsed : Visibility.Visible; } return converterParameter == "Reverse" ? Visibility.Visible : Visibility.Collapsed; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { var converterParameter = parameter?.ToString(); if (value == null) { return Visibility.Visible; } if ((Visibility)value == Visibility.Visible) { return converterParameter == "Reverse" ? false : true; } return converterParameter == "Reverse" ? true : false; } } } }