Skip to content

Instantly share code, notes, and snippets.

@wtuts
Created June 22, 2014 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wtuts/d63e0b8dfb2d226fccf1 to your computer and use it in GitHub Desktop.
Save wtuts/d63e0b8dfb2d226fccf1 to your computer and use it in GitHub Desktop.
boolean to visibility converter class
public class BooleanToVisibilityConverter : IValueConverter
{
//Set to true if you want to show control when boolean value is true
//Set to false if you want to hide/collapse control when value is true
private bool triggerValue = false;
public bool TriggerValue
{
get { return triggerValue; }
set { triggerValue = value; }
}
//Set to true if you just want to hide the control
//else set to false if you want to collapse the control
private bool isHidden;
public bool IsHidden
{
get { return isHidden; }
set { isHidden = value; }
}
private object GetVisibility(object value)
{
if (!(value is bool))
return DependencyProperty.UnsetValue;
bool objValue = (bool)value;
if ((objValue && TriggerValue && IsHidden) || (!objValue && !TriggerValue && IsHidden))
{
return Visibility.Collapsed;
}
if ((objValue && TriggerValue && !IsHidden) || (!objValue && !TriggerValue && !IsHidden))
{
return Visibility.Collapsed;
}
return Visibility.Visible;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return GetVisibility(value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment