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 Window Start-Up Locations

WPF windows can initially be displayed using three positioning options. The location of a window can be set manually, or the new window can be centred on the screen or in relation to its owner.


In the Windows Presentation Foundation Fundamentals tutorial, I described many properties of the Window class, which controls the display of windows and dialog boxes. The articles described how you can position a window manually using the Left and Top properties, which set the window's location relative to the top-left corner of the screen.

In many cases, you will want to centralise a window, either on the screen or relative to its owner. You could do this by obtaining the resolution of the screen, or the location and size of the parent window, calculating the correct location and setting the Left and Top properties. However, you can perform this type of positioning automatically by setting the WindowStartupLocation property to a constant from the WindowStartupLocation enumeration.

To demonstrate, create a new WPF application project in Visual Studio. Name the project, "WindowStartupLocationDemo". Once the solution is prepared, replace the XAML in the main window with the code below:

<Window x:Class="WindowStartupLocationDemo.MainWindow"
        Height="250" Width="450"
        Left="10" Top="20"
        <Button Canvas.Left="120" Canvas.Top="100" Width="100" Click="Screen_Click">
            Centre Screen
        <Button Canvas.Left="230" Canvas.Top="100" Width="100" Click="Owner_Click">
            Centre Owner

The above XAML creates a window with two buttons, each with its Click event registered. Switch to the code behind the window and ensure that the empty Click methods appear as follows:

private void Screen_Click(object sender, RoutedEventArgs e)


private void Screen_Click(object sender, RoutedEventArgs e)


Centring Windows

A common option for a newly loaded window is to centralise it on the screen. This can help to guide the user's attention to important items. Centralising on the screen is generally used when the window has no obvious owner or parent.

To open a new window and have it centred on the screen, set the WindowStartupLocation property to CenterScreen. You can do this using the XAML or in code before the window is displayed.

Let's configure the "Centre Screen" button to load a new instance of the main window in the middle of the screen. To do so, update the button's Click method, as follows:

private void Screen_Click(object sender, RoutedEventArgs e)
    var window = new MainWindow();
    window.WindowStartupLocation = WindowStartupLocation.CenterScreen;

Run the program and click the button to see the result.

Centring in an Owning Window

When you display a child window from an existing window, it can be useful to centralise the child relative to its parent. As the user should be looking at the parent, their attention should be quickly drawn to the child.

To centre a window in this way, the new window's Owner property should be set to the parent window's object. You also set the WindowStartupLocation property to CenterOwner.

To demonstrate, update the second click method, as shown below. Note that after the MainWindow object is created, the Owner is set and the start-up location option is selected. The child window is slightly smaller than its parent, as if it was the same size, it would obscure the original window.

private void Owner_Click(object sender, RoutedEventArgs e)
    const double MinWidth = 200;
    const double MinHeight = 200;

    double width = this.Width - 50;
    double height = this.Height - 30;

    var window = new MainWindow();
    window.Owner = this;
    window.Width = width > MinWidth ? width : MinWidth;
    window.Height = height > MinHeight ? height : MinHeight;
    window.WindowStartupLocation = WindowStartupLocation.CenterOwner;

Run the program again to try the new code.

19 February 2016