Book Image

Windows Presentation Foundation Development Cookbook

Book Image

Windows Presentation Foundation Development Cookbook

Overview of this book

Windows Presentation Foundation (WPF) is Microsoft's development tool for building rich Windows client user experiences that incorporate UIs, media, and documents. With the updates in .NET 4.7, Visual Studio 2017, C# 7, and .NET Standard 2.0, WPF has taken giant strides and is now easier than ever for developers to use. If you want to get an in-depth view of WPF mechanics and capabilities, then this book is for you. The book begins by teaching you about the fundamentals of WPF and then quickly shows you the standard controls and the layout options. It teaches you about data bindings and how to utilize resources and the MVVM pattern to maintain a clean and reusable structure in your code. After this, you will explore the animation capabilities of WPF and see how they integrate with other mechanisms. Towards the end of the book, you will learn about WCF services and explore WPF's support for debugging and asynchronous operations. By the end of the book, you will have a deep understanding of WPF and will know how to build resilient applications.
Table of Contents (13 chapters)
2
Using WPF Standard Controls

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.