Click here to Skip to main content
15,881,681 members
Articles / Desktop Programming / WPF
Alternative
Tip/Trick

All purpose Boolean to Visibility Converter

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
18 Nov 2011CPOL 17.4K   2   3
I would first like to say that I have been through the same situation, but never took the time to develop a more dynamic converter. I like your idea, but don't like the implementation. If I know what I want to happen (when the value is true then make the control hidden), I have to do some mental...
I would first like to say that I have been through the same situation, but never took the time to develop a more dynamic converter. I like your idea, but don't like the implementation. If I know what I want to happen (when the value is true then make the control hidden), I have to do some mental translation to Boolean values (TriggerValue="True" IsHidden="True").

I think this solution is cleaner and more clear about what it is doing. Also, thanks to Paulo Zemek for recommending the null value.

C#
/// <summary>
	/// Converts Boolean Values to Control.Visibility values
	/// </summary>
	public class BooleanToVisibilityConverter : IValueConverter
	{
		private Visibility valueWhenTrue = Visibility.Visible;
		public Visibility ValueWhenTrue
		{
			get { return valueWhenTrue; }
			set { valueWhenTrue = value; }
		}

		private Visibility valueWhenFalse = Visibility.Collapsed;
		public Visibility ValueWhenFalse
		{
			get { return valueWhenFalse; }
			set { valueWhenFalse = value; }
		}

		private Visibility valueWhenNull = Visibility.Hidden;
		public Visibility ValueWhenNull
		{
			get { return valueWhenNull; }
			set { valueWhenNull = value; }
		}

		private object GetVisibility(object value)
		{
			if (!(value is bool) || value == null)
				return ValueWhenNull;

			if ((bool)value)
				return valueWhenTrue;

			return valueWhenFalse;
		}

		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();
		}
	}


Using your examples:

<!--Hides control if boolean value is true-->
               <local:BooleanToVisibilityConverter x:Key="HiddenIfTrue" ValueWhenTrue="Hidden" ValueWhenFalse="Visible"/>
               <!--Hides control if boolean value is false-->
               <local:BooleanToVisibilityConverter x:Key="HiddenIfFalse" ValueWhenTrue="Visible" ValueWhenFalse="Hidden"/>
               <!--Collapses control if boolean value is true-->
               <local:BooleanToVisibilityConverter x:Key="CollapsedIfTrue" ValueWhenTrue="Collapsed" ValueWhenFalse="Visible"/>
               <!--Collapses control if boolean value is false-->
               <local:BooleanToVisibilityConverter x:Key="CollapsedIfFalse" ValueWhenTrue="Visible" ValueWhenFalse="Collapsed"/>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 Great work! Pin
Wayne Gaylard18-Nov-11 17:05
professionalWayne Gaylard18-Nov-11 17:05 
GeneralThat's great. I think I prefer your implemetation. Mine was ... Pin
Wayne Gaylard18-Nov-11 3:44
professionalWayne Gaylard18-Nov-11 3:44 
GeneralRe: Thanks! Sometimes it's good to know I make things better an ... Pin
rhuiden18-Nov-11 3:49
rhuiden18-Nov-11 3:49 

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

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