using System; using System.Collections.Generic; using System.Globalization; using System.Text; using System.Windows; using System.Windows.Data; using System.Windows.Markup; namespace Flyinsono.Client.Test.Extensions { [Flags] public enum BoolToVisibilityEnums { /// /// True=Visible,False=Collapsed /// None = 0x0, /// /// True=invisible,False=visible /// Revert = 0x2, /// /// invisible=Visibility.Hidden /// InvisibleHidden = 0x4, RevertHidden = Revert | InvisibleHidden } [MarkupExtensionReturnType(typeof(IValueConverter))] public class BoolToVisibilityConverterExtension : MarkupExtension, IValueConverter, IMultiValueConverter { #region Properties  [ThreadStatic] private static BoolToVisibilityConverterExtension _converter; public static BoolToVisibilityConverterExtension Converter { get { return _converter ?? (_converter = new BoolToVisibilityConverterExtension()); } } #endregion Properties  #region Methods  // Constructors  public BoolToVisibilityConverterExtension() { } // Methods  public override object ProvideValue(IServiceProvider serviceProvider) { return Converter; } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var boolValue = ToBool(value); bool isRevert; Visibility invisible; TryParseParameter(parameter, out invisible, out isRevert); if (isRevert) { boolValue = !boolValue; } return boolValue ? Visibility.Visible : invisible; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { var visibility = (Visibility)value; return visibility == Visibility.Visible; } public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { bool boolValue = false; if (values != null && values.Length > 0) { bool hasInvisible = false; foreach (var value in values) { if (!ToBool(value)) { hasInvisible = true; break; } } boolValue = !hasInvisible; } bool isRevert; Visibility invisible; TryParseParameter(parameter, out invisible, out isRevert); if (isRevert) { boolValue = !boolValue; } return boolValue ? Visibility.Visible : invisible; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } private bool ToBool(object value) { var boolValue = false; if (value != null) { if (value is Visibility) { boolValue = (Visibility)value == Visibility.Visible; } else { string valueInString = value.ToString(); if (string.IsNullOrEmpty(valueInString) || !bool.TryParse(valueInString, out boolValue)) { boolValue = false; } } } return boolValue; } private void TryParseParameter(object parameter, out Visibility invisible, out bool isRevert) { isRevert = false; invisible = Visibility.Collapsed; if (parameter is BoolToVisibilityEnums) { BoolToVisibilityEnums options = (BoolToVisibilityEnums)parameter; if ((options & BoolToVisibilityEnums.Revert) != 0) { isRevert = true; } if ((options & BoolToVisibilityEnums.InvisibleHidden) != 0) { invisible = Visibility.Hidden; } } } #endregion Methods  } }