Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Windows Presentation Foundation Development Cookbook

You're reading from   Windows Presentation Foundation Development Cookbook 100 recipes to build rich desktop client applications on Windows

Arrow left icon
Product type Paperback
Published in Feb 2018
Publisher Packt
ISBN-13 9781788399807
Length 524 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Kunal Chowdhury Kunal Chowdhury
Author Profile Icon Kunal Chowdhury
Kunal Chowdhury
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
1. WPF Fundamentals FREE CHAPTER 2. Using WPF Standard Controls 3. Layouts and Panels 4. Working with Data Bindings 5. Using Custom Controls and User Controls 6. Using Styles, Templates, and Triggers 7. Using Resources and MVVM Patterns 8. Working with Animations 9. Using WCF Services 10. Debugging and Threading 11. Interoperability with Win32 and WinForm 1. Other Books You May Enjoy Index

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:

  1. Right-click on the project node and select Add | Window... from the context menu. The Add New Item dialog will be shown on the screen.
  2. Select Window (WPF) from the available list, give it the name ToolBox, and click Add to continue. This will add ToolBox.xaml and ToolBox.xaml.cs into your project.
  3. 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> 
  1. Now open the App.xaml page and remove the property attribute StartupUri, defined as (StartupUri="MainWindow.xaml") from it.
  2. Go to its code-behind file App.xaml.cs and override the OnStartup event. We need to modify the implementation according to our needs. Replace the entire OnStartup 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(); 
} 
  1. Run the application to see the relationship between the two windows. The windows will look like the following screenshot:
  2. Drag the ToolBox window and you can see that you are able to move it outside the MainWindow. Now perform some operations, such as minimizing and closing, on the MainWindow, and you will see that the ToolBox 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 to null
You have been reading a chapter from
Windows Presentation Foundation Development Cookbook
Published in: Feb 2018
Publisher: Packt
ISBN-13: 9781788399807
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime
Visually different images