BlackWaspTM

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 - Ownership

The one hundred and seventieth part of the Windows Presentation Foundation Fundamentals tutorial continues to look at the Window control. This article explains how one window can be set as the owner of another, and the effects of ownership.

Window Ownership

Depending upon their use, you may want the windows in your applications to be completely independent or to be linked together using ownership. When one window owns another, a relationship is defined between them. The owning and owned windows behave differently than if they were two independent items. There are several new behaviours and features:

  • The window that has an owner can reference the owning window, gaining access to the owner's properties and other members.
  • When the owner window is closed, all the owned windows are closed automatically. The Closing event of the owned windows is not raised.
  • If the user minimises a window, all of the windows that it owns are also minimised. Minimising an owned window does not minimise the owner.
  • An owner window cannot appear in front of a window that it owns, even if the owner is activated.
  • When minimised, if an owner window is restored or maximised, any minimised, owned windows are restored.

In this article we will see how to set up the ownership relationship between windows and how to access the items that a window owns. To demonstrate, we need a new sample project. Create a new WPF Application in Visual Studio. Name the project, "WindowOwnerDemo". Once the solution is ready, replace the XAML in the main window with the following code:

<Window x:Class="WindowOwnerDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="1" Height="150" Width="200">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <Button Click="OpenWindow_Click">Open Child</Button>
        <Button Grid.Row="1">Close Owned</Button>
    </Grid>
</Window>

The window includes two buttons. The first button's Click event is linked to a method that will open a new window. Change the code behind the window so that the MainWindow class opens a new window each time the "Open Window" button is clicked:

public partial class MainWindow : Window
{
    int lastChild = 0;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void OpenWindow_Click(object sender, RoutedEventArgs e)
    {
        var window = new MainWindow();
        window.Title = Title + "/" + (++lastChild).ToString();
        window.Show();
    }
}

Run the program and try clicking the "Open Window" button. Each click opens a new instance of the main window with a different title. The title includes the title of the window that opened it, with an extra number appended. The windows are not linked; closing one window does not close any other, windows can be minimised and maximised individually and any window can be positioned in front of any other.

WPF Window Ownership Demo Window

Setting a Window's Owner

To specify the owner of a window, you simply set the Owner property of the new window before it is displayed. The owning window must have already been shown to avoid an exception being thrown.

Let's modify the code behind the "Open Window" button so that the newly opened windows are owned by the window that shows them. Update the method, as follows, to set the Owner property before calling Show:

private void OpenWindow_Click(object sender, RoutedEventArgs e)
{
    var window = new MainWindow();
    window.Title = Title + "/" + (++lastChild).ToString();
    window.Owner = this;
    window.Show();
}

Run the program again and launch some new windows. Try minimising and closing the owners to see the effects on the owned windows. Try moving a newly opened window in front of its owner, then activating the owner to see that it does not the window that it showed. When you have finished, close the window with the title, "1". Every window will close and the program will exit.

Accessing Owned Windows

The owned windows can access their owner using the Owner property. In some situations you will need to access the details of owned windows from the code of their owner. For example, before you close an owner, you may wish to check that all of the linked windows are in a suitable state to also be closed. If you don't, the user might lose data. Handily, whenever you set the Owner property of a window, a reference to the owned window is added to the OwnedWindows property of its owner. You can access the items in this collection individually or iterate through them all.

Let's add the code for the second button in the window. We will loop through all of the items in the OwnedWindows collection when the user clicks the button, closing each in turn. Start by updating the XAML for the button to register the Click event:

<Button Grid.Row="1" Click="CloseOwnedWindows_Click">Close Owned</Button>

Next, add the following method behind the window. This uses a foreach loop to iterate through the child windows. The Close method is called against each, closing the window and any other windows that it owns.

private void CloseOwnedWindows_Click(object sender, RoutedEventArgs e)
{
    foreach (Window window in OwnedWindows)
    {
        window.Close();
    }
}

Run the program and open several child windows. Click the "Close Owned" button to see that all of the owned windows close.

4 July 2015