Export (0) Print
Expand All

Window Class

Represents an out-of-browser application window.

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

public class Window : DependencyObject

The Window type exposes the following members.

  NameDescription
Public methodWindowInitializes a new instance of the Window class.
Top

  NameDescription
Public propertyContentGets or sets the root visual element that represents the contents of the window.
Public propertyDispatcherGets the Dispatcher this object is associated with. (Inherited from DependencyObject.)
Public propertyHeightGets or sets the height of the application window in pixels.
Public propertyIsActiveGets a value that indicates whether the window is active.
Public propertyIsVisibleGets a value that indicates whether the window is currently visible in the user interface.
Public propertyLeftGets or sets the position of the left edge of the application window; see Remarks for restrictions on setting this property at run time.
Public propertyTitleGets or sets the window title bar text.
Public propertyTopGets or sets the position of the top edge of the application window; see Remarks for restrictions on setting this property at run time.
Public propertyTopMostGets or sets a value that indicates whether the application window is always displayed in front of other windows.
Public propertyVisibilityGets or sets a value that indicates whether the window is currently visible or collapsed.
Public propertyWidthGets or sets the width of the application window in pixels.
Public propertyWindowStateGets or sets a value that indicates whether the window is maximized, minimized, or in the normal state.
Public propertyWindowStyleGets or sets a value that indicates the appearance of the title bar and border of the Window.
Top

  NameDescription
Public methodActivateAttempts to activate the application window by bringing it to the foreground and setting the input focus to it.
Public methodCheckAccessDetermines whether the calling thread has access to this object. (Inherited from DependencyObject.)
Public methodClearValueClears the local value of a dependency property. (Inherited from DependencyObject.)
Public methodCloseCloses the application window unless the Closing event is canceled.
Public methodDragMoveIn trusted applications, begins a mouse drag operation to move the window.
Public methodDragResizeIn trusted applications, begins a mouse drag operation to move the specified window edge or corner.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetAnimationBaseValueReturns any base value established for a Silverlight dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetValueReturns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject.)
Public methodStatic memberGetWindowGets the window that contains the specified DependencyObject.
Public methodHideChanges the value of the Visibility property of the Window to Collapsed.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodReadLocalValueReturns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.)
Public methodSetValueSets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject.)
Public methodShowChanges the value of the Visibility property of the Window to Visible.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public eventClosingOccurs when the window is about to close.
Top

You can use this class to programmatically manipulate an out-of-browser application window at run time. However, in Silverlight 4, an application can have only one window, which is created automatically. To retrieve this Window instance, use the Application.MainWindow property. You can use this instance only if the application is currently running outside the browser. The properties of this instance are initialized from the corresponding WindowSettings properties in the application manifest.

You can set the properties of this class only in response to user-initiated actions unless the current application is a trusted application. For example, you can change the window size in a button Click event handler.

In trusted applications, you can remove the window title bar and border, and optionally, use rounded corners. For more information, see How to: Configure an Application for Out-of-Browser Support.

Without a title bar and border, however, there are no Minimize, Maximize, and Close buttons; and users cannot drag the window or its edges. You can replace the missing functionality by using the WindowState property and the Close, DragMove, and DragResize methods.

In Silverlight 5, trusted, out-of-browser applications can create multiple Window instances and manipulate them programmatically. This is useful to support flexible, multi-monitor layouts and to create features such as non-modal dialog boxes and tear-off windows.

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. At a minimum, you must configure the instance by setting its Height, Width, and Content properties.

You cannot create a Window instance in XAML, nor create a Window subclass. However, you will typically set the Content property to a new instance of a user control that you define in XAML, as shown in the example.

On creation, a Window instance is automatically added to the Application.Windows collection, where you can access it later. This includes the Application.MainWindow instance, which is always the first item in the collection.

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.

The Application.MainWindow value differs from additional Window instances in the following ways:

  • The main window is created automatically and configured using settings in the application manifest.

  • The main window controls the lifetime of all other windows. When the main window closes, all additional windows close automatically.

  • The Visibility property has a default value of Visible for the main window and Collapsed for all other windows.

  • The TopMost property is meaningful only for the main window.

  • Only the main window can have a WindowStyle property value of BorderlessRoundCornersWindow.

The following Silverlight 5 code examples show how a trusted, out-of-browser application can display an arbitrary user control in a separate window. These examples require a UserControl subclass named DemoUserControl.

The following code example shows how to create a window that will be used one time. Each time you create a new window with this code, it is added to the Application.Windows collection and given a unique Title based on the number of windows currently in the collection.


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 single window multiple times, you must set its Visibility property and cancel its Closing event, as shown in the following code example.


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, 4

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft