Saturday, April 17, 2010

WPF: Inherit from a base window

In a little project I was working on I've decided to implement a base form (or window since it's WPF..) to contain some mutual attributes logical & visual - simplest inherit.
After a little research I've encounter a few articles discussing the implementation and started working...

My application is a navigation WPF application so instead of the window I was working with page.

So I started with the base page, changed the "Window" class to "Page" both in XAML and the actual class:

<Page x:Class="TestBasePage.BasePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test Base Page" Height="300" Width="300">
<Grid>
<Label Name="label1">Testing</Label>
</Grid>
</Page>

public partial class BasePage : Page
{
public BasePage()
{
InitializeComponent();
}
}


Than added the 1st child page and changed the base class to be our new "BasePage":

public partial class childPage : BasePage
{
public childPage()
{
InitializeComponent();
}
}


And in XAML, a little bit messier:

<src:BasePage x:Class="TestBasePage.childPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:TestBasePage"
Title="childPage">
<Grid>

</Grid>
</src:BasePage>


The result of this experiment was:

'TestBasePage.BasePage' cannot be the root of a XAML file because it was defined using XAML.

After a few hours spent looking for what I was doing wrong I found out that visual inherit is not supported in WPF?!

In ASP.NET 2.0 the "master form" was one of the biggest "hu-ha"'s, in WinForms it was always there...Can it be?? really? such a basic functionality is not supported?

All the articles & blogs I found on the subject concluded that inherit is possible but only for the class part not for the XAML stuff...

Well...maybe in WPF 4.0..didn't get there yet...

The alternative is of course using a user control - it will do the trick - but it's still wierd.

If you have different information on this, you are invited to comment.

Till next time.
Diego

PS: here are some of the links I found on this:

WPF: Inheriting from custom class instead of Window

WPF (XAML) Visual Inheritance by Umit Gunduz

WPF: build error MC6017 when you define a class that derives from a XAML generated class