Window.Visibility Property

Silverlight

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)

public Visibility Visibility { get; set; }

Property Value

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

ExceptionCondition
NotSupportedException

The application is not running outside the browser.

ArgumentException

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

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.

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 && 
    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 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;
    }
}


Silverlight

Supported in: 5

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

Community Additions

ADD
Show: