Skip to content

Creating a new Window

Grega Mohorko edited this page Dec 21, 2017 · 5 revisions

Follow the steps bellow after adding a new Window (WPF) to your project.

Modifying the XAML file

  • Open up MyWindow.xaml
  • Change the parent class to BaseWindow:
    • Add a reference to the Windows namespace: xmlns:windows="clr-namespace:GM.WPF.Windows;assembly=GM.WPF"
    • Replace the opening tag <Window ... with <windows:BaseWindow ...
  • [optional] If you have a ViewModel, add this attribute to the opening tag: d:DataContext="{d:DesignInstance Type=local:MyWindowViewModel, IsDesignTimeCreatable=True}"

Your XAML file should look something like this:

<windows:BaseWindow x:Class="MyWpfApplication.MyWindow"
        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:windows="clr-namespace:GM.WPF.Windows;assembly=GM.WPF"
        mc:Ignorable="d"
        d:DataContext="{d:DesignInstance Type=local:MyWindowViewModel, IsDesignTimeCreatable=True}"
        >
    <!-- The content -->
</windows:BaseWindow>

Modifying the CodeBehind file

  • Open up MyWindow.xaml.cs
  • Change the parent class to BaseWindow

It should look something like this:

using GM.WPF.Windows;

namespace MyWpfApplication
{
    public partial class MyWindow : BaseWindow
    {
        public MyWindow()
        {
            InitializeComponent();
        }
    }
}

If your window implements IDisposable, it will automatically be disposed when it closes.

Clone this wiki locally