This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Controls - Window - Basics

The one hundred and sixty-eighth part of the Windows Presentation Foundation Fundamentals tutorial looks at the Window class, which is used to generated windows containing other controls.

Window Size and Position

You can control the position and size of a window using properties that are inherited from FrameworkElement. We've seen these before so I won't describe them in detail here. The key properties are:

  • Left. The distance between the left side of the desktop and the left edge of the window.
  • Top. The distance between the top of the desktop and the top edge of the window.
  • Width. The width of the entire window, including the width of any borders.
  • Height. The height of the entire window, including the height of any borders and the title bar.
  • MinWidth. The minimum permitted width for the window.
  • MaxWidth. The maximum permitted width for the window.
  • MinHeight. The minimum permitted height for the window.
  • MaxHeight. The maximum permitted height for the window.

You can use the properties to set the location and size of the window or to determine its current location and dimensions. In addition, FrameworkElement defines the SizeChanged event, which fires when the size is modified, and Window adds the LocationChanged event, to detect the window moving.

Let's register the two events to see them in action. Update the opening tag of the Window element first:

<Window x:Class="WindowDemo.MainWindow"
        Title="Window Demo" Height="150" Width="200"
        WindowState="Maximized" StateChanged="Window_StateChanged"
        LocationChanged="Window_LocationChanged" SizeChanged="Window_SizeChanged">

Switch to the code behind the window to add the methods to handle the events. Remove the existing Window_StateChanged method and add the following code instead:

private void Window_StateChanged(object sender, EventArgs e)

private void Window_SizeChanged(object sender, SizeChangedEventArgs e)

private void Window_LocationChanged(object sender, EventArgs e)

private void UpdateMessage()
    string msg = string.Format("{0} ({1},{2}) ({3},{4})",
        WindowState, Left, Top, Width, Height);
    Message.Text = msg;

The updated program now shows the current window state, location and size whenever any of the relevant properties are changed. Run the program and try moving and resizing the window to see the results.

WPF Toolbar Window with Size and Location changes detected

Automatically Sizing a Window to Fit its Content

The final property to consider in this article configures the window to size to its content automatically. Usually, the SizeToContent property is only used when the window is not resizable, as the automatic sizing is disabled if the user resizes the window. To enable the feature, you set the SizeToContent property to a value from the SizeToContent enumeration. Four options are available:

  • Manual. This is the default option. The size of the window is controlled using the properties that we've already described above. If the property is set to any other option and the user resizes the window, the value resets to Manual.
  • Height. The width of the window is controlled using the Width property but the height changes to fit to the size of the child controls.
  • Width. The height of the window is controlled using the Height property but the width changes to fit to the size of the child controls.
  • WidthAndHeight. Both the height and the width of the window are resized automatically to match the size of its contents.
27 June 2015