Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

All purpose Boolean to Visibility Converter

0.00/5 (No votes)
20 Nov 2011 1  
All purpose Boolean to Visibility Converter
If you have ever had to bind a controls Visibility property to a boolean value, you have probably used Microsoft's built in BooleanToVisibilityConverter. The only problem with this is that you can only use it to convert True boolean values to Visibility.Collapsed. If you want it any other way, you need to provide your own Converter. In any major application, you always tended to end up with 4 different converters to do one simple job.

  1. Convert True values to Collapsed
  2. Convert False values to Collapsed
  3. Convert True values to Hidden
  4. Convert False values to Hidden


So I decided to create one converter that does all four jobs, and I thought I would share it.

The converter class:
C#
/// <summary>
    /// Converts Boolean Values to Control.Visibility values
    /// </summary>
    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.Hidden;
            }
            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();
        }
    }


Usage:
XML
<!--Hides control if boolean value is true-->
               <local:BooleanToVisibilityConverter x:Key="HiddenIfTrue" TriggerValue="True" IsHidden="True"/>
               <!--Hides control if boolean value is false-->
               <local:BooleanToVisibilityConverter x:Key="HiddenIfFalse" TriggerValue="False" IsHidden="True"/>
               <!--Collapses control if boolean value is true-->
               <local:BooleanToVisibilityConverter x:Key="CollapsedIfTrue" TriggerValue="True" IsHidden="False"/>
               <!--Collapses control if boolean value is false-->
               <local:BooleanToVisibilityConverter x:Key="CollapsedIfFalse" TriggerValue="False" IsHidden="False"/>


If the boolean value you are using to bind to is a nullable type, then it will return a DependancyProperty.UnsetValue if the value returns null, and will set the control to Visible.

That's it. I hope you find this as useful as I did.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here