0 out of 5 rated this helpful - Rate this topic

BooleanToVisibilityConverter Class

Represents the converter that converts Boolean values to and from Visibility enumeration values.

System.Object
  System.Windows.Controls.BooleanToVisibilityConverter

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
[LocalizabilityAttribute(LocalizationCategory.NeverLocalize)]
public sealed class BooleanToVisibilityConverter : IValueConverter
<BooleanToVisibilityConverter .../>

The BooleanToVisibilityConverter type exposes the following members.

  Name Description
Public method BooleanToVisibilityConverter Initializes a new instance of the BooleanToVisibilityConverter class.
Top
  Name Description
Public method Convert Converts a Boolean value to a Visibility enumeration value.
Public method ConvertBack Converts a Visibility enumeration value to a Boolean value.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top

Use the BooleanToVisibilityConverter class to convert a Boolean to and from a Visibility value. The Convert method returns Visibility.Visible when true is passed in or Visibility.Collapsed when false is passed in. Note that the Visibility.Collapsed value hides the control and does not reserve space for it in a layout. When you call the ConvertBack method and specify a reference to an object, it returns true if the object is Visible; otherwise, it returns false.

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Here is an example
Since there wasn't an example, I wrote one.

Binding Visibility to a bool value in WPF
http://www.rhyous.com/2011/02/22/binding-visibility-to-a-bool-value-in-wpf/
Why does it HAVE to collapse?
I wrote my own version of this (mainly because I didn't know this one existed at the time!).  One thing mine does that has come in handy is allow you to designate what "Not visible" (false) means.  Maybe someone else will find it useful: $0 $0--------------------------------------------------------------$0 $0 $0$0 $0$0 $0
    [ValueConversion(typeof(bool), typeof(Visibility))]
    public class BooleanVisibilityConverter : IValueConverter
    {
        #region Constructors

        public BooleanVisibilityConverter()
            : this(true)
        { }

        public BooleanVisibilityConverter(bool collapseWhenInvisible)
            : base()
        {
            CollapseWhenInvisible = collapseWhenInvisible;
        }

        #endregion

        #region Properties

        public bool CollapseWhenInvisible { get; set; }

        public Visibility FalseVisibility
        {
            get
            {
                if (CollapseWhenInvisible)
                    return Visibility.Collapsed;
                else
                    return Visibility.Hidden;
            }
        }

        #endregion

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return Visibility.Visible;
            if ((bool)value)
                return Visibility.Visible;
            else
                return FalseVisibility;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return true;
            return ((Visibility)value == Visibility.Visible);
        }

        #endregion
    }
$0
$0$0 $0------------------------------------------------------------------$0 $0$0 $0 $0The usage would look something like this:$0 $0$0 $0 $0$0 $0
<my:BooleanVisibilityConverter CollapseWhenInvisible="True" x:Key="_CollapseVisibilityConverter" /> <!-- This one would behave the same as Microsoft's implementation -->

<my:BooleanVisibilityConverter CollapseWhenInvisible="False" x:Key="_CollapseVisibilityConverter" /> <!-- This one would free up the layout space occupied by the control that has it's visibility set to false -->
$0
$0$0 $0 $0 $0$0 $0 $0$0 $0