Export (0) Print
Expand All
Expand Minimize
1 out of 2 rated this helpful - Rate this topic

Visibility Enumeration

Specifies the display state of an element.

Namespace:  System.Windows
Assembly:  PresentationCore (in PresentationCore.dll)
public enum Visibility
<object property="enumerationMemberName" .../>
Member nameDescription
VisibleDisplay the element.
HiddenDo not display the element, but reserve space for the element in layout.
CollapsedDo not display the element, and do not reserve space for it in layout.

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">
	    <DockPanel>
	
		    <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>    
            <StackPanel HorizontalAlignment="Center">
                <TextBox Name="tb1" Width="100" Height="50">A TextBox</TextBox>
                <TextBlock Name="txt1" TextWrapping="Wrap" FontSize="14"/>
            </StackPanel>
        </DockPanel>
	</Border>

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

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

private 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.";
}

private 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 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.