What is the window class that you get automatically when you create a new WPF application in Visual Studio? A WPF window is a combination of a XAML (.xaml/”zammel”) file, where the
I will create a project using Visual Studio 2017 Community so that we can look at an example. I will call the project MyWPFApp. I’m using .NET Framework version 4.6.1. Below is the XAML file, MainWindow.xaml that you get when you create this project.
<Window x:Class="MyWPFApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:MyWPFApp" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> </Grid> </Window>
In the first line of code you will see that the class attribute includes the name of the project that we used when we created the application (MyWPFApp) and “MainWindow”. “MainWindow” was given to us. The second part tells XAML the name of the class to use, which is MainWindow. Below is the class. It is in a file called MainWindow.xaml.cs.
using System.Windows; namespace MyWPFApp { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } } }
The MainWindow class is defined as partial because it’s being combined with your XAML file in runtime to give you the full window. This is actually what the call to InitializeComponent() does, which is why it’s required to get a full functioning window up and running.
Changing the Namespace Name and the Class Name
If we wanted to change the namespace and class names by adding an underscore 2 at the end of each, here is how we would do that. The class is namespace dot classname. We can change the namespace and class name without doing anything with the solution or project name. They can stay the same.
<Window x:Class="MyWPFApp_2.MainWindow_2"
namespace MyWPFApp_2 { public partial class MainWindow_2 : Window { public MainWindow_2()
Window Properties
Over at WPF Tutorial there is anarticle on The Window class. They list some of the more interesting properties of the window class at the bottom of that article.