UIElement.Visibility Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets the visibility of a UIElement. A UIElement that is not visible does not render and does not communicate its desired size to layout.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Property Visibility As Visibility
public Visibility Visibility { get; set; }
<uiElement Visibility="Visible"/>
-or-
<uiElement Visibility="Collapsed"/>

Property Value

Type: System.Windows.Visibility
A value of the enumeration. The default value is Visibility.Visible.

Remarks

Dependency property identifier field: VisibilityProperty

Elements where Visibility is not Visible do not participate in input events, are not in a tab sequence (cannot be focused), and are excluded during hit testing.

NoteNote:

The value of IsHitTestVisible is ignored when an object is not set to Visible. IsHitTestVisible is a settable property that enables you to disable hit testing for objects that would otherwise be hit testable by virtue of the Visibility and Background property values.

The reason that Visibility uses an enumeration rather than a simple Boolean is that this area of the Silverlight object model is modeled on the Windows Presentation Foundation (WPF) Visibility property, which uses a three-state model for Visibility of Visible/Hidden/Collapsed. In the WPF model, Hidden denotes a visibility state where the object should not render, but should still occupy space in a WPF layout. Silverlight does not support Hidden. If you are importing XAML UI definitions from WPF, you might need to change the cases where a Visibility is declared as Hidden in order to use that XAML in Silverlight.

For more information on the XAML syntax, see Visibility.

Visibility in a Visual State

As part of defining visual states for a control, you will sometimes want to change the Visibility state of an object to Collapsed. Visual states rely on animations. The property value type of UIElement.Visibility is Visibility, an enumeration. To animate values that are enumerations, you must use a DiscreteObjectKeyFrame. (You also use this technique for Boolean values).

The following XAML example shows a visual state that uses DiscreteObjectKeyFrame to change visibility.

<VisualState x:Name="Focused">
  <Storyboard>
    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Visibility" Duration="0">
      <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
    </ObjectAnimationUsingKeyFrames>
  </Storyboard>
</VisualState>

For more information on the visual state model for controls, see Creating a New Control by Creating a ControlTemplate.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.