Click here to Skip to main content
Click here to Skip to main content

Tagged as

All purpose Boolean to Visibility Converter

, 20 Nov 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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:
/// <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:
<!--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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Wayne Gaylard
Software Developer DynaByte Solutions
Zimbabwe Zimbabwe
No Biography provided

Comments and Discussions

 
QuestionNice, best solution I have found that is reusable and makes sense. PinmemberAndy Missico3-Mar-14 12:08 
AnswerRe: Nice, best solution I have found that is reusable and makes sense. PinmentorWayne Gaylard3-Mar-14 18:32 
GeneralReason for my vote of 5 I am giving you a five because I did... PinmvpPaulo Zemek17-Nov-11 5:18 
GeneralRe: Thanks! PinmemberWayne Gaylard17-Nov-11 5:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.1 | Last Updated 20 Nov 2011
Article Copyright 2011 by Wayne Gaylard
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid