Created
July 31, 2014 19:26
-
-
Save idiotandrobot/9c14b2aa2e0a6b5ec959 to your computer and use it in GitHub Desktop.
Bool to Visibility converters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BoolToInvisibilityConverter : IValueConverter | |
{ | |
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) | |
{ | |
bool isInvisible = (bool)value; | |
if (BoolToVisibilityConverter.IsVisibilityInverted(parameter)) | |
isInvisible = !isInvisible; | |
return (isInvisible ? Visibility.Collapsed : Visibility.Visible); | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) | |
{ | |
bool isInvisible = ((Visibility)value == Visibility.Collapsed); | |
if (BoolToVisibilityConverter.IsVisibilityInverted(parameter)) | |
isInvisible = !isInvisible; | |
return isInvisible; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BoolToVisibilityConverter : IValueConverter | |
{ | |
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) | |
{ | |
bool isVisible = (bool)value; | |
if (IsVisibilityInverted(parameter)) | |
isVisible = !isVisible; | |
return (isVisible ? Visibility.Visible : Visibility.Collapsed); | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) | |
{ | |
bool isVisible = ((Visibility)value == Visibility.Visible); | |
if (IsVisibilityInverted(parameter)) | |
isVisible = !isVisible; | |
return isVisible; | |
} | |
internal static bool IsVisibilityInverted(object parameter) | |
{ | |
return (GetVisibilityMode(parameter) == Visibility.Collapsed); | |
} | |
internal static Visibility GetVisibilityMode(object parameter) | |
{ | |
Visibility mode = Visibility.Visible; | |
if (parameter != null) | |
{ | |
if (parameter is Visibility) | |
{ | |
mode = (Visibility)parameter; | |
} | |
else | |
{ | |
try | |
{ | |
mode = (Visibility)Enum.Parse(typeof(Visibility), parameter.ToString(), true); | |
} | |
catch (FormatException ex) | |
{ | |
throw new FormatException("Invalid Visibility specified as the ConverterParameter. Use Visible or Collapsed.", ex); | |
} | |
} | |
} | |
return mode; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
via http://jeffhandley.com/archive/2008/10/27/binding-converters---visibilityconverter.aspx