Window Class

Definition

Provides the ability to create, configure, show, and manage the lifetime of windows and dialog boxes.

[System.Windows.Localizability(System.Windows.LocalizationCategory.Ignore)]
public class Window : System.Windows.Controls.ContentControl
Inheritance
Derived
Attributes

Inherited Members

System.Object

System.Windows.Controls.ContentControl

System.Windows.Controls.Control

System.Windows.DependencyObject

System.Windows.FrameworkElement

System.Windows.Media.Visual

System.Windows.Threading.DispatcherObject

System.Windows.UIElement

Examples

The following example shows how a standard window is defined using only markup:

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    Title="Main Window in Markup Only" 
    Height="300" 
    Width="300" />

The following example shows how a standard window is defined using only code:

using System;
using System.Windows;

namespace CSharp
{
    public partial class CodeOnlyWindow : Window
    {
        public CodeOnlyWindow()
        {
            this.Title = "Main Window in Code Only";
            this.Width = 300;
            this.Height = 300;
        }
    }
}

Imports System
Imports System.Windows

Namespace VisualBasic
    Partial Public Class CodeOnlyWindow
        Inherits Window
        Public Sub New()
            Me.Title = "Main Window in Code Only"
            Me.Width = 300
            Me.Height = 300
        End Sub
    End Class
End Namespace

The following example shows how a standard window is defined using a combination of markup and code-behind.

<Window 
    x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Main Window" 
    Height="300" 
    Width="300" />
using System;
using System.Windows;
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

Imports System
Imports System.Windows
Partial Public Class MainWindow
	Inherits Window
	Public Sub New()
		InitializeComponent()
	End Sub
End Class

Remarks

The point of interaction between a user and a standalone application is a window. A Windows Presentation Foundation (WPF) window consists of two distinct areas:

  • A non-client area, which hosts the windows adornments, including an icon, title, System menu, minimize button, maximize button, restore button, close button, and a border.

  • A client area, which hosts application-specific content.

A standard window is shown in the following figure:

Window elements

Window encapsulates the ability to create, configure, show, and manage the lifetime of both windows and dialog boxes, and provides the following key services:

Lifetime Management: Activate, Activated, Close, Closed, Closing, Deactivated, Hide, IsActive, Show, SourceInitialized.

Window Management: GetWindow, OwnedWindows, Owner.

Appearance and Behavior: AllowsTransparency, ContentRendered, DragMove, Icon, Left, LocationChanged, ResizeMode, RestoreBounds, ShowActivated, ShowInTaskbar, SizeToContent, StateChanged, Title, Top, Topmost, WindowStartupLocation, WindowState, WindowStyle

Dialog Boxes: DialogResult, ShowDialog.

Additionally, Application exposes special support for managing all of the windows in an application:

  • Application maintains a list of all the windows that are currently instantiated in the application. This list is exposed by the Windows property.

  • By default, MainWindow is automatically set with a reference to the first Window that is instantiated in an application. This thereby making the window the main application window.

A Window can be implemented using markup, markup and code-behind, or code.

Window is primarily used to display windows and dialog boxes for standalone applications. However, for applications that require navigation at the window level, such as wizards, you can use NavigationWindow instead; NavigationWindow derives from Window and extends it with browser-style navigation support.

Note

Islands of navigable content can be incorporated into other content and content containers using Frame.

Window needs UnmanagedCode security permission to be instantiated. This has the following consequences:

  • ClickOnce-deployed standalone applications will request permission elevation when launched from either the Internet or Local Intranet zones.

  • XBAPs that request anything less than full permissions will not be able to instantiate windows or dialog boxes.

For information about standalone application deployment and security considerations, see WPF Security Strategy - Platform Security.

A Window is a ContentControl, which means that it can contain a single object of any type (such as a string, an image, or a panel). For more information, see the ContentControl class. Also, Window is a root element and, therefore, cannot be part of another element's content.

Note

The Height, Width, Top, and Left properties set on a Window through a style will not be applied at run time.

Customizing the Window Control

To apply the same property settings to multiple Window controls, use the Style property. You can modify the default ControlTemplate to give the control a unique appearance. For more information about creating a ControlTemplate, see Customizing the Appearance of an Existing Control by Creating a ControlTemplate. To see the parts and states that are specific to the Window, see Window Styles and Templates.

Dependency properties for this control might be set by the control’s default style. If a property is set by a default style, the property might change from its default value when the control appears in the application. The default style is determined by which desktop theme is used when the application is running. For more information, see Default WPF Themes.

Note

Setting a visual property will only have an effect if that property is both present in Window control's default template and is set by using a . You can find a list of visual properties in the "Changing the Visual Structure of a Control" section in Customizing the Appearance of an Existing Control by Creating a ControlTemplate.

Constructors

Window()

Initializes a new instance of the Window class.

Fields

Allows​Transparency​Property

Identifies the AllowsTransparency dependency property.

Dpi​Changed​Event

A RoutedEvent for when the DPI of the screen the Window is on changes.

Icon​Property

Identifies the Icon dependency property.

Is​Active​Property

Identifies the IsActive dependency property.

Left​Property

Identifies the Left dependency property.

Resize​Mode​Property

Identifies the ResizeMode dependency property.

Show​Activated​Property

Identifies the ShowActivated dependency property.

Show​In​Taskbar​Property

Identifies the ShowInTaskbar dependency property.

Size​To​Content​Property

Identifies the SizeToContent dependency property.

Taskbar​Item​Info​Property

Identifies the TaskbarItemInfo dependency property.

Title​Property

Identifies the Title dependency property.

Topmost​Property

Identifies the Topmost dependency property.

Top​Property

Identifies the Top dependency property.

Window​State​Property

Identifies the WindowState dependency property.

Window​Style​Property

Identifies the WindowStyle dependency property.

Properties

Allows​Transparency

Gets or sets a value that indicates whether a window's client area supports transparency.

Dialog​Result

Gets or sets the dialog result value, which is the value that is returned from the ShowDialog() method.

Icon

Gets or sets a window's icon.

Is​Active

Gets a value that indicates whether the window is active.

Left

Gets or sets the position of the window's left edge, in relation to the desktop.

Logical​Children

Gets an enumerator for a window's logical child elements.

Owned​Windows

Gets a collection of windows for which this window is the owner.

Owner

Gets or sets the Window that owns this Window.

Resize​Mode

Gets or sets the resize mode.

Restore​Bounds

Gets the size and location of a window before being either minimized or maximized.

Show​Activated

Gets or sets a value that indicates whether a window is activated when first shown.

Show​In​Taskbar

Gets or sets a value that indicates whether the window has a task bar button.

Size​To​Content

Gets or sets a value that indicates whether a window will automatically size itself to fit the size of its content.

Taskbar​Item​Info

Gets or sets the Windows 7 taskbar thumbnail for the Window.

Title

Gets or sets a window's title.

Top

Gets or sets the position of the window's top edge, in relation to the desktop.

Topmost

Gets or sets a value that indicates whether a window appears in the topmost z-order.

Window​Startup​Location

Gets or sets the position of the window when first shown.

Window​State

Gets or sets a value that indicates whether a window is restored, minimized, or maximized.

Window​Style

Gets or sets a window's border style.

Methods

Activate()

Attempts to bring the window to the foreground and activates it.

Arrange​Override(​Size)

Override this method to arrange and size a window and its child elements.

Close()

Manually closes a Window.

Drag​Move()

Allows a window to be dragged by a mouse with its left button down over an exposed area of the window's client area.

Get​Window(​Dependency​Object)

Returns a reference to the Window object that hosts the content tree within which the dependency object is located.

Hide()

Makes a window invisible.

Measure​Override(​Size)

Override this method to measure the size of a window.

On​Activated(​Event​Args)

Raises the Activated event.

On​Closed(​Event​Args)

Raises the Closed event.

On​Closing(​Cancel​Event​Args)

Raises the Closing event.

On​Content​Changed(​Object, ​Object)

Called when the Content property changes.

On​Content​Rendered(​Event​Args)

Raises the ContentRendered event.

On​Create​Automation​Peer()

Creates and returns a WindowAutomationPeer object for this Window.

On​Deactivated(​Event​Args)

Raises the Deactivated event.

On​Dpi​Changed(​Dpi​Scale, ​Dpi​Scale)

Called when the DPI at which this window is rendered changes.

On​Location​Changed(​Event​Args)

Raises the LocationChanged event.

On​Manipulation​Boundary​Feedback(​Manipulation​Boundary​Feedback​Event​Args)

Called when the ManipulationBoundaryFeedback event occurs.

On​Source​Initialized(​Event​Args)

Raises the SourceInitialized event.

On​State​Changed(​Event​Args)

Raises the StateChanged event.

On​Visual​Parent​Changed(​Dependency​Object)

Called when the parent of the window is changed.

Show()

Opens a window and returns without waiting for the newly opened window to close.

Show​Dialog()

Opens a window and returns only when the newly opened window is closed.

Events

Activated

Occurs when a window becomes the foreground window.

Closed

Occurs when the window is about to close.

Closing

Occurs directly after Close() is called, and can be handled to cancel window closure.

Content​Rendered

Occurs after a window's content has been rendered.

Deactivated

Occurs when a window becomes a background window.

Dpi​Changed

Occurs after the DPI of the screen on which the Window is displayed changes.

Location​Changed

Occurs when the window's location changes.

Source​Initialized

This event is raised to support interoperation with Win32. See HwndSource.

State​Changed

Occurs when the window's WindowState property changes.