123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- 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
- {
- /// <summary>
- /// True=Visible,False=Collapsed
- /// </summary>
- None = 0x0,
- /// <summary>
- /// True=invisible,False=visible
- /// </summary>
- Revert = 0x2,
- /// <summary>
- /// invisible=Visibility.Hidden
- /// </summary>
- 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
- }
- }
|