Creating ownership between windows
In the WPF application, the window objects that you create are independent of each other by default. But, sometimes, you may want to create an owner-owned relationship between them. For example, the toolbox window that you generally see in your Visual Studio IDE and/or in a Photoshop application.
When you set an owner of a window, it acts according to the owner instance. For example, if you minimize or close the owner window, the other window under the owner-owned relationship automatically minimizes or closes according to its owner.
Let's begin creating this recipe to have an owner-owned relationship between two windows.
Getting ready
To get started with this recipe, open your Visual Studio IDE and create a new WPF project called CH01.OwnershipDemo
.
How to do it...
Perform the following steps to create a ToolBox
window and assign its ownership to the MainWindow
, so that it can act according to its owner:
- Right-click on the project node and select
Add
|Window...
from the context menu. TheAdd New Item
dialog will be shown on the screen. - Select
Window (WPF)
from the available list, give it the nameToolBox
, and clickAdd
to continue. This will addToolBox.xaml
andToolBox.xaml.cs
into your project. - Open the
ToolBox.xaml
file and replace its content with the following XAML code:
<Window x:Class="CH01.OwnershipDemo.ToolBox" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" SizeToContent="WidthAndHeight" ResizeMode="NoResize" Title="ToolBox"> <StackPanel Margin="10"> <Button Content="Bold" Width="70" Margin="4"/> <Button Content="Italics" Width="70" Margin="4"/> <Button Content="Underlined" Width="70" Margin="4"/> </StackPanel> </Window>
- Now open the
App.xaml
page and remove the property attributeStartupUri
, defined as (StartupUri="MainWindow.xaml"
) from it. - Go to its code-behind file
App.xaml.cs
and override theOnStartup
event. We need to modify the implementation according to our needs. Replace the entireOnStartup
event handler with the following code block:
protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); var mainWindow = new MainWindow(); mainWindow.Show(); // must show before setting it as owner of some other window var toolBox = new ToolBox { Owner = mainWindow }; toolBox.Show(); }
- Run the application to see the relationship between the two windows. The windows will look like the following screenshot:
- Drag the
ToolBox
window and you can see that you are able to move it outside theMainWindow
. Now perform some operations, such as minimizing and closing, on theMainWindow
, and you will see that theToolBox
window also acts according to its owner.
How it works...
By default, the owner of every Window
object is set to null, and thus each window is independent of the other. But, when you set its owner, it follows the owner-owned relationship and acts with the owner window.
Window
ownership is not a feature of WPF, but a capability of the Win32 user API and, accessible from a WPF application.
There's more...
Make sure you display the owner window first, before setting it as the owner of some other window, otherwise the system will throw an InvalidOperationException
:

Some points to note about window ownership:
- The window that has an ownership relationship with another window always appears on top of that owner
- You can drag the window outside the owner window
- When you minimize or close the owner, the other window, which is related to it, will follow the owner and minimize or close respectively
- By default, the window in a relationship gets displayed in the taskbar, but when you minimize the owner, it gets removed from the taskbar
- When you want to break the relationship, just set the
Owner
property tonull