Understanding WPF deriving WIndow class

Your base class should just be a class file (not a Window).

So create WindowBase.cs

public class WindowBase : Window
{
    // ...
}

In MainWindow (for example) change the xaml.cs file to inherit from WindowBase instead

public partial class MainWindow : WindowBase
{
    public MainWindow()
    {
        InitializeComponent();
    }
    // ...
}

In MainWindow.xaml, include the namespace for WindowBase and change Window to base:WindowBase like this

<base:WindowBase x:Class="SubclassWindow.MainWindow"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:base="clr-namespace:NamespaceForWindowBase"
                  Title="MainWindow" Height="350" Width="525">
    <!--...-->
</base:WindowBase>

Leave a Comment