Visibility Enumeration

Specifies the display state of an element.

Namespace: System.Windows
Assembly: PresentationCore (in presentationcore.dll)
XML Namespace:

public enum Visibility : byte
public enum Visibility
public enum Visibility
<object property="EnumerationValue" .../>

 Member nameDescription
CollapsedDo not display the element, do not reserve space for it in layout. 
HiddenDo not display the element, but reserve space for the element in layout. 
VisibleDisplay the element. 

Elements that have a Visibility value of Collapsed do not occupy any layout space. By default, elements are Visible.

This example shows how to change the value of the Visibility property.

The first example, which is Extensible Application Markup Language (XAML), defines a TextBox and places it in a StackPanel element. Three Button controls represent the three enumeration values of the Visibility property: Visible, Hidden, and Collapsed. When a user clicks any of these buttons, the click triggers an event in the code-behind file that is associated with the Button. The Visibility state of the TextBox is changed to match the value associated with the Button.

<Border BorderBrush="Black" BorderThickness="2" Background="White">

        <TextBlock FontSize="20" FontWeight="Bold" DockPanel.Dock="Top" Margin="0,0,0,10">UIElement.Visibility Sample</TextBlock>
        <TextBlock DockPanel.Dock="Top" Margin="0,0,0,10">Click the buttons below to manipulate the Visibility property of the TextBox below.</TextBlock>
        <StackPanel DockPanel.Dock="Left">
            <Button Name="btn1" Height="25" Click="contentVis">Visibility="Visible"</Button>
            <Button Name="btn2" Height="25" Click="contentHid">Visibility="Hidden"</Button>
            <Button Name="btn3" Height="25" Click="contentCol">Visibility="Collapsed"</Button> 
        <StackPanel HorizontalAlignment="Center">
            <TextBox Name="tb1" Width="100" Height="50">A TextBox</TextBox>
            <TextBlock Name="txt1" TextWrapping="Wrap" FontSize="14"/>

The following code-behind file handles the ButtonClick events that the previous XAML example defines.

public void contentVis(object sender, RoutedEventArgs e)
    tb1.Visibility = System.Windows.Visibility.Visible;
    txt1.Text = "Visibility is now set to Visible.";

public void contentHid(object sender, RoutedEventArgs e)
    tb1.Visibility = System.Windows.Visibility.Hidden;
    txt1.Text = "Visibility is now set to Hidden. Notice that the TextBox still occupies layout space.";

public void contentCol(object sender, RoutedEventArgs e)
    tb1.Visibility = System.Windows.Visibility.Collapsed;
    txt1.Text = "Visibility is now set to Collapsed. Notice that the TextBox no longer occupies layout space.";

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions