WPF Controls - Window - Display and Activation
The one hundred and sixty-ninth part of the Windows Presentation Foundation Fundamentals tutorial continues to describe the Window class. This article looks at the members related to opening, closing, activating and deactivating windows.
In the previous article in the WPF tutorial we looked at some of the basic properties of the Window control, which allows you to create and display windows, We've used the Window control as a container throughout the tutorial whilst demonstrating the operation of various controls. In every example we have had a single window that is displayed automatically when the program starts. This is the default behaviour for new WPF application projects.
Applications are rarely limited to a single window. They include different windows for different activities, ranging from simple message boxes and dialog boxes to complex windows that allow you to provide information, edit documents and facilitate many other tasks. Multiple windows can be open simultaneously and the user can switch between them to achieve their goals.
In this article we will see how you create new window objects and display them to the user. We'll also see how to activate, close and hide windows programmatically, as well as detecting when some of these events occur.
To demonstrate the methods, properties and events required, we need a sample project. Create a new WPF application in Visual Studio, naming the new solution, "WindowDisplayDemo". Once the solution is prepared, replace the XAML of the main window with the following code:
Title="Window Demo" Height="150" Width="250"
FontSize="20" Text="Main Window"/>
<Button Grid.Row="1" Width="150" Height="40">Open Window</Button>
The above creates a window containing a text block and a button. We will configure the button so that clicking it opens another window of a different design. The first thing we need to do is to add the new window to the solution. To do so, right-click the project name in the Solution Explorer. A context-sensitive menu should appear. Select the "Add" option, then click "Window". This starts the process of adding a new window, using the standard dialog box for adding items.
Enter the name, "SecondWindow" for the new file and click the "Add" button. The new window will be added to the project and displayed in the Solution Explorer. It should be opened automatically, allowing you to edit its XAML. Replace the XAML with the following:
Title="Window Demo" Height="150" Width="250"
The second window includes a text box and two buttons. We'll use the three controls to demonstrate some of the display and activation members of the Window class. We'll also see one way to transfer information between windows, using the text box contents.
The most important action that we will look at in this article is displaying a new window. Often this is the only operation required, as users will generally be able to close windows themselves. Opening a new window is a two part process. Firstly, you create an instance of the class for the window. New window objects are not displayed automatically, which means that you can initialise them whilst hidden, then show them later, avoiding screen updates that may be confusing. Once initialised, you call the Show method to make the new window visible.
Let's show a new instance of SecondWindow each time the "Open Window" button in the main window is clicked. Start by registering the Click event against the button, as follows:
<Button Grid.Row="1" Width="150" Height="40" Click="Open_Click">Open Window</Button>
Next we need to add the code for the event behind the main window. Add the following method:
private void Open_Click(object sender, RoutedEventArgs e)
var secondWindow = new SecondWindow();
The method starts by creating a new instance, then shows it. We could combine the two statements into a single line. However, using two lines makes it easy to understand what is happening. In addition, you could insert lines after the instantiation, but before the call to Show, in order to set up the properties of the new window, such as the data context.
Run the program to see the results. Click the "Open Window" button several times. Each click should open a new window with the SecondWindow design. As each of these is a separate object, they are independent of each other.
To close the program, close all of the windows individually or stop debugging within Visual Studio.
30 June 2015