15

Possible Duplicate:
Inheriting from a UserControl in WPF

I'm trying to create a WPF user control, which is derived from an other user control, which I also have defined.

<myNamespace:NavigationControl
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:myNamespace="clr-namespace:myNamespace" mc:Ignorable="d"
x:Class="myNamespace.WelcomeScreen"
x:Name="UserControl"
d:DesignWidth="640" d:DesignHeight="480">

and here's what WelcomeScreen.cs looks like:

public partial class WelcomeScreen : NavigationControl
{
    public WelcomeScreen()
    {
        this.InitializeComponent();
    }
}

When I compile this, I get following error:

'myNamespace.NavigationControl' cannot be the root of a XAML file because it was defined using XAML.

What am I doing wrong?

Community
  • 1
  • 1
Arsen Zahray
  • 24,367
  • 48
  • 131
  • 224
  • for WPF workaround with Visual inheritance see: http://svetoslavsavov.blogspot.gr/2009/09/user-control-inheritance-in-wpf.html or for explicitly defining the GUI in the ancestor see http://support.microsoft.com/kb/957231 – George Birbilis Jan 10 '15 at 01:44
  • 1
    I don't think this is an exact duplicate... the other question doesn't mention this error message at all. They are certainly related, but this one is much more specific. The other one, in fact, I don't think is even clear what was being asked. – StayOnTarget Jan 27 '21 at 17:25

2 Answers2

17

See the MS Knowledgebase Article: http://support.microsoft.com/kb/957231 . Deriving user controls in XAML is not supported; you must derive user controls in code.

10

You are not meant to do that and the error tells you that.

Besides, using XAML you set the Content, if you set it again in your derived control you would lose everything you defined before. If you want inheritance and reusability use a custom control (and create a respective ControlTemplate).

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • 4
    Well this sucks. In UWP, Silverlight, and Xamarin Forms, this is completely normal. It seems WPF is missing the ability to do this. – Christian Findlay Apr 08 '18 at 23:58
  • 2
    There's a big assumption in the answer here. The assumption is that if you are trying to set the Content when using the base class in the root of the XAML. That's not necessarily the case. – Christian Findlay Apr 09 '18 at 01:14
  • 1
    "And the error tells you that". -- you are seeing things. – Mike Nakis Jul 18 '20 at 11:36
  • Just to clarify: you can inherit with UserControl or Window like this, as long as the base class does not have an associated xaml file. If you just want a base Window/UserControl class that provides common helper functions/properties, create it as a regular class (instead of as a UserControl/Window) and then add the base class with code, rather than using the `New->UserControl` menu. – caesay Jul 08 '21 at 17:31
  • The error only makes sense if you've seen and resolved it before. If it's your first time seeing the error, it's a bit cryptic on what is wrong and how to fix it. The microsoft kb article linked to in the other answer was what I needed to resolve the error. – purplecat May 19 '22 at 20:53