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

To create a new window, follow these simple steps:

  1. Open the Solution Explorer and right-click on the project node.
  2. From the right-click context menu, navigate to Add | Window... as shown in the following screenshot:
  1. The following Add New Item dialog will appear on the screen:
  1. Make sure that the selected template is Window (WPF). Give it a name, SecondWindow.xaml, and click the Add button.
  2. This will create the SecondWindow.xaml file and its associated code-behind file SecondWindow.xaml.cs in the project directory.
  3. Open the XAML file (SecondWindow.xaml) and replace the entire contents with the following XAML code:
<Window x:Class="CH01.WindowDemo.SecondWindow" 
  xmlns=
"http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Second Window" Height="200" Width="300"> <Grid> <TextBlock Text="Second Window Instance" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="16"/> </Grid> </Window>
  1. Now open the MainWindow.xaml file to add a button into it. Replace the entire <Grid> </Grid> block, with the following XAML code:
<Grid> 
    <Button Content="Open Second Window" 
            Height="30" Width="150" 
            Click="OnSecondWindowButtonClicked"/> 
</Grid> 
  1. Now we need to add the implementation for the button-click event. Simply open the MainWindow.xaml.cs file and add the following lines of code inside the class definition:
private void OnSecondWindowButtonClicked(object sender,  
RoutedEventArgs e) 
{ 
    var window = new SecondWindow(); 
    window.Show(); 
} 
  1. Now, when you run the application, you will see that the MainWindow opens on the screen, containing a button labeled Open Second Window. Clicking on this button opens the second window on the screen that has text content of Second Window Instance. Here's the screenshot for your reference:
Please note that if you click the button again, it will create another instance of the second window because it's modeless.