DRapp
DRapp

Reputation: 48179

wpf derive from derived control

I want to derive a control from a derived user control but having problems with the xaml resolving it. However, first the simple scenario. I have a library MyControls.dll. In that, I have a control based on "UserControl" in a .cs file such as

public class MyUserControl : UserControl { ... }

No problem. Now, I want to create a second (VISUAL) control derived from this... So, I do a new UserControl called NewFromMyUserControl.xaml.cs and NewFromMyUserControl.cs respectively in this same MyControls.dll library such as

public partial class NewFromMyUserControl : MyUserControl { ... }

Now, in the Xaml, I have...

<mylib:MyUserControl x:Class="MyControls.SubFolder1.NewFromMyUserControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mylib ="clr-namespace:MyControls" (blah blah) />
</mylib:MyUserControl>

This works no problem. And for simplicity purposes, no actual code in the "MyUserControl" or the "NewFromMyUserControl" classes... just testing derived implementation.

NOW, the problem. I have another library "MyBaselineControls.dll" which has the actual UserControl I want derived from... such as

public class MyBaselineUserControl : UserControl { ... }

and Now, I want to change the "MyUserControl" to be derived from this other such as

from
    public class MyUserControl : UserControl { ... }
to
    using MyBaselineControls;
    public class MyUserControl : MyBaselineUserControl { ... }

As soon as this one line changed, the compiler fails stating...

Error 2 'MyControls.MyUserControl' cannot be the root of a XAML file because it was defined using XAML. Line 1 Position 19. ... blah blah

Are there issues of deriving from derived of another library?

Thanks

Upvotes: 2

Views: 5010

Answers (1)

Adi Lester
Adi Lester

Reputation: 25211

You can't derive from user controls that have a XAML definition. As long as you don't define XAML in your base class, you'll be fine. Specific information about your error can be found here.

One way to work around it is to derive from Control (or another class derived from Control which is not UserControl) and define a control template for your base and derived controls. This page explains exactly what you can do when you inherit from Control or UserControl, and which things you should consider when deciding on which you'd like to derive from.


EDIT

Since you're saying that both MyBaselineUserControl and MyUserControl don't define any XAML, things should work. I tried it myself and I had no problems.

I recommend that you make sure that your base UserControls were actually created with no XAML, i.e. they should have been created with "Add --> Class" and not with "Add --> User Control".

Upvotes: 3

Related Questions