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.

Resize Modes

Windows support several resizing modes. Changing the mode allows you to restrict the user's ability to resize, maximise and minimise your window. For most windows you might use the default option, which allows the user to freely resize. For windows with fixed content, you might elect to prevent resizing but allow the window to be minimised. For dialog boxes that require an immediate response, you would normally disable minimising too.

The resize mode is set using the ResizeMode property, which requires a value from the ResizeMode enumeration. Four options are available:

  • CanResize. Allows all available resizing options. The user can resize the window by dragging its borders, and can maximise and minimise it. This is the default option.
  • CanResizeWithGrip. This option provides the same resizing functionality as CanResize. In addition, a sizing grip is added to the bottom-right corner of the window. This is a small triangular area that is easier for the user to target with the mouse pointer when resizing.
  • CanMinimize. With this option, the window can no longer be resized freely. Dragging the borders of the window has no effect and maximising is not possible. The maximise button is disabled or hidden.
  • NoResize. When the resize mode is set to NoResize, the window size is fixed. No resizing, maximising or minimising is permitted. The maximise and minimise buttons are removed from the title bar.

Setting the Window's Style

Before we change the resize mode, let's also consider the window style, as they are related. The window style determines the appearance of the borders of the window, the title bar and the icons and buttons that are shown. The appearance is also controlled by the resize mode and the version of the operating system in use.

The window's style is set using the WindowStyle property and a value from the WindowStyle enumeration. Four options are available:

  • SingleBorderWindow. The default option includes all standard buttons and a single, flat border.
  • ThreeDBorderWindow. This option provides the same functionality as SingleBorderWindow. The difference is the style of the border, which is three-dimensional. Depending upon the operating system version, the first two styles can appear very similar or even identical.
  • ToolWindow. This option is designed for use with floating toolbars. The borders and title bar have a different style. The icon and the maximise and minimise buttons are removed, even if the resize mode would normally show them.
  • None. The title bar is removed, leaving only the content area. Borders are shown if the resize mode is configured to allow manual resizing. If the resize mode is set to CanMinimize or NoResize, the borders are removed completely. This style is useful when you wish to create your own custom window chrome or design windows that are not rectangular.

Let's change the ResizeMode and WindowStyle properties of our example window. The code below adds a sizing grip whilst switching to the toolbar window style.

<Window x:Class="WindowDemo.MainWindow"
        Title="Window Demo" Height="150" Width="200"
        ResizeMode="CanResizeWithGrip" WindowStyle="ToolWindow">
    <Label Name="MessageLabel" FontSize="25">Hello, World!</Label>

Run the program to see the difference in style. Note the triangular sizing grip in the bottom corner of the window, the border style changes and the removal of the icon, minimise and maximise buttons.

WPF Toolbar Window with Sizing Grip

Window States

When a window is displayed, it appears in one of three possible states. These are maximised, when the window fills the screen, normal, where the window may be manually resizable, or minimised, when the window is not visible but usually has a button in the task bar. You can detect the state or change it using the WindowState property, which holds a value from the WindowState enumeration.

As well as changing or checking the window state using the property, you can detect when the user changes the state using the standard operating system actions. This is useful if you wish to change the layout for different window sizes, or if you have set the WindowStyle to None and created your own, custom chrome. To detect changes, subscribe to the StateChanged event.

Let's demonstrate the property and the event. First, update the XAML for the Window to set the property to Maximized. This means that the window will fill the screen when it is first opened. We'll also link the StateChanged event to a method.

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

Next, switch to the code behind the window and add the method to handle the StateChanged event. This code changes the text in the TextBlock to show the current state.

private void Window_StateChanged(object sender, EventArgs e)
    Message.Text = WindowState.ToString();

Run the program to see the results. The window should be maximised initially. If you restore and maximise the window, the text will update to show the current state.

27 June 2015