There is no spoon
There is no spoon

Reputation: 1796

c# UserControl base class inheritance

I have a base class call PopupWindow which inherits from UserControl. PopupWindow has no associated xaml page, its just a regular class which inherits from UserControl

I then have another UserControl which inherits from PopupWindow. Now this is a proper user control in that is has an associated xaml page.

I have changed the root xaml tag to be PopupWindow like this

<local:PopupWindow
  .....
</local:PopupWindow>

where local is the namespace in this PopupWindow exists.

But I keep getting an error that PopupWindow does not exist in the given namespace.

What am I doing wrong?

Thanks

Upvotes: 3

Views: 2130

Answers (1)

Rob
Rob

Reputation: 1030

I've managed to get this working as follows:

Parent control:

public class PopupWindow : UserControl
{
}

Inheriting control:

Code behind:

public partial class PopupWindowChild
{
    public PopupWindowChild()
    {
        InitializeComponent();
    }
}

XAML:

<Controls:PopupWindow x:Class="MyNamespace.Controls.PopupWindowChild"
                      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                      xmlns:Controls="clr-namespace:MyNamespace.Controls">
    <TextBlock Text="Blah" />
</Controls:PopupWindow>

Main view:

<Window x:Class="MyNamespace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Controls="clr-namespace:MyNamespace.Controls">
    <Controls:PopupWindow />
</Window>

Your XAML won't recognise your control until the code is built. Once built and run, I'm seeing "Blah" in my main view as expected.

Upvotes: 2

Related Questions