Window.Visibility Property

Gets or sets a value that indicates whether the window is currently visible or collapsed.

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

Syntax

'Declaration
Public Property Visibility As Visibility
public Visibility Visibility { get; set; }

Property Value

Type: System.Windows.Visibility
A value that indicates whether the window is currently visible or collapsed.

Exceptions

Exception Condition
NotSupportedException

The application is not running outside the browser.

ArgumentException

When setting this property, the specified value is not a valid Visibility value.

Remarks

Setting this property affects the value of IsVisible.

In Silverlight 5, trusted, out-of-browser applications can create multiple Window instances and manipulate them programmatically. To create and display a new window, you must programmatically call the Window() constructor, configure the instance, and then set its Visibility property to Visible, as shown in the Example section below.

When a window is closed either by the user or by the Close method, the instance is removed from the Windows collection and the instance becomes invalid. If you want to reuse a Window instance, handle the Closing event, set Cancel to true, and then hide the window by setting its Visibility property to Collapsed.

Caution noteCaution:

Setting this property to Collapsed for the Application.MainWindow instance can leave your application running, but inaccessible.

Examples

The following Silverlight 5 code example shows how a trusted, out-of-browser application can display an arbitrary user control in a separate window. This example requires a UserControl subclass named DemoUserControl.

If (Application.Current.IsRunningOutOfBrowser AndAlso _ 
    Application.Current.HasElevatedPermissions)

    Dim newWindow As New Window With
    {
        .Title = "Demo Window # " &
            Application.Current.Windows.Count.ToString(),
        .Height = 300,
        .Width = 300,
        .Top = 0,
        .Left = 0,
        .Content = New DemoUserControl(),
        .Visibility = Visibility.Visible
    }
End If
if (Application.Current.IsRunningOutOfBrowser && 
    Application.Current.HasElevatedPermissions)
{
    var newWindow = new Window()
    {
        Title = "Demo Window # " + 
            Application.Current.Windows.Count.ToString(),
        Height = 300, 
        Width = 300, 
        Top = 0, 
        Left = 0,
        Content = new DemoUserControl(),
        Visibility = Visibility.Visible
    };
}

By default, you cannot reuse a window after it is closed. To display and hide a window multiple times, you must set its Visibility property and cancel its Closing event, as shown in the following code example. This example requires a UserControl subclass named DemoUserControl. Additionally, this example will work only in a trusted, out-of-browser application targeting Silverlight 5.

Private Sub ShowHideDemoWindowButton_Click(
    sender As System.Object, e As System.Windows.RoutedEventArgs)

    If (Not Application.Current.IsRunningOutOfBrowser OrElse _ 
        Not Application.Current.HasElevatedPermissions) Then Return

    DemoWindow.Visibility = If(DemoWindow.IsVisible,
        Visibility.Collapsed, Visibility.Visible)

End Sub

Private _demoWindow As Window
Private ReadOnly Property DemoWindow As Window
    Get
        If (Not Application.Current.IsRunningOutOfBrowser OrElse
            Not Application.Current.HasElevatedPermissions) Then Return Nothing

        If _demoWindow Is Nothing Then
            _demoWindow = New Window With
            {
                .Title = "Demo Window",
                .Content = New DemoUserControl(),
                .Height = 300, .Width = 300, .Top = 0, .Left = 0
            }

            AddHandler _demoWindow.Closing,
                Sub(sender As Object,
                    e As System.ComponentModel.ClosingEventArgs)
                    If (e.IsCancelable) Then
                        e.Cancel = True
                        _demoWindow.Visibility = Visibility.Collapsed
                    End If
                End Sub
        End If

        Return _demoWindow
    End Get
End Property
private void ShowHideDemoWindowButton_Click(object sender, RoutedEventArgs e)
{
    if (!Application.Current.IsRunningOutOfBrowser || 
        !Application.Current.HasElevatedPermissions) return;
    DemoWindow.Visibility = DemoWindow.IsVisible ?
        Visibility.Collapsed : Visibility.Visible;
}

private Window _demoWindow;
private Window DemoWindow
{
    get
    {
        if (!Application.Current.IsRunningOutOfBrowser || 
            !Application.Current.HasElevatedPermissions) return null;

        if (_demoWindow == null)
        {
            _demoWindow = new Window()
            {
                Title = "Demo Window",
                Content = new DemoUserControl(),
                Height = 300, Width = 300, Top = 0, Left = 0
            };

            _demoWindow.Closing += (sender, e) =>
            {
                if (e.IsCancelable)
                {
                    e.Cancel = true;
                    _demoWindow.Visibility = Visibility.Collapsed;
                }
            };
        }

        return _demoWindow;
    }
}

Version Information

Silverlight

Supported in: 5

Platforms

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