Creating a window
Windows are the typical top level controls in WPF. By default, a MainWindow
class is created by the application wizard and automatically shown upon running the application. In this recipe, we'll take a look at creating and showing other windows that may be required during the lifetime of an application.
Getting ready
Make sure Visual Studio is up and running.
How to do it...
We'll create a new class derived from Window
and show it when a button is clicked:
Create a new WPF application named
CH05.NewWindows
.Right-click on the project node in Solution explorer, and select Add | Window…:
In the resulting dialog, type
OtherWindow
in the Name textbox and click on Add.A file named
OtherWindow.xaml
should open in the editor. Add aTextBlock
to the existingGrid
, as follows:<TextBlock Text="This is the other window" FontSize="20" VerticalAlignment="Center" HorizontalAlignment="Center" />
Open
MainWindow.xaml
. Add aButton
to theGrid
with aClick
event handler:<Button Content...