Search
Close this search box.

WPF: Inheriting from custom class instead of Window

In ASP.NET, we learned that it is often interesting to inherit from another class than from System.Web.UI.Page. This allows to define common methods, such as utilities, etc… which are used by a set of web pages throughout an application.

In WPF, it’s also possible to do the same, and to inherit from a custom class instead of System.Windows.Window, of System.Windows.Controls.Page, or of System.Windows.Controls.UserControl for example.

When you add a Window (or Page, or UserControl…) to a WPF project, the chain of inheritance is as follows:

If you have a special method “doSomething()” which you want to reuse in every Window in the application, then you can store it in an abstract class GalaSoftLb.Wpf.WindowBase and modify the inheritance as follows:

In the diagrams above, the Window class is the framework’s one, and the class GalaSoftLb.Wpf.WindowBase is abstract.

In order to get the new Windows to derive from this abstract class, not only the C# code must be modified, but also the XAML code. This is a little tricky, because a special syntax must be used. Instead of the usual:

<Window x:Class="WindowsApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="WindowsApplication1" Height="300" Width="300"
  >
  <Grid>        
  </Grid>
</Window>

We have instead:

<src:WindowBase x:Class="WindowsApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:src="clr-namespace:GalaSoftLb.Wpf" 
  Title="WindowsApplication1" Height="300" Width="300"
  >
  <Grid>
  </Grid>
</src:WindowBase>

I

nstead of deriving from Window, we derive from WindowBase. However, WindowBase is in an unknown namespace, so we must tell the XAML parser where to look for this class. This is done using the “xmlns:src” line. “xmlns” is for “xml namespace”. “src” is a prefix, you are free to choose any qualifier you want. Typically, we use “src” for source, or “clr” for common language runtime, but this is up to you. It must be unique in this XML document.

Instead of specifying a URL like we normally do in XML, we use the syntax “clr-namespace” to define the src namespace. This means that the XAML parser will look into the Common Language Runtime, and look into a namespace named GalaSoftLb.Wpf to find the class WindowBase.

Note: The XML namespace and the CLR namespace are two different notions, we are at the crossroad of two worlds here. It’s important to understand what these two namespaces represent.

Often, the base class is stored in an external class library, in order to reuse it in different WPF applications. In this case, the syntax becomes:

<src:WindowBase x:Class="WindowsApplication2.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:src="clr-namespace:GalaSoftLb.Wpf;assembly=GalaSoftLb.Wpf" 
  Title="WindowsApplication2" Height="300" Width="300"
>

Here, we tell the XAML parser to look for the namespace GalaSoftLb.Wpf into the DLL GalaSoftLb.Wpf.DLL, which must be referenced in the project.

In the code behind, there are no surprises:

public partial class Window1 : GalaSoftLb.Wpf.WindowBase

Finally a note about the implementation: If you want to store the abstract WindowBase class in a class library, you must add the following .NET libraries to the list of References: PresentationFramework, PresentationCore, WindowsBase.

This article is part of the GWB Archives. Original Author: Laurent Bugnion

Related Posts