3

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

There is no spoon
  • 1,775
  • 2
  • 22
  • 53

1 Answers1

2

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.

Rob
  • 981
  • 12
  • 27
  • Thats great you got it working. What if PopupWindow uses generics, i.e public class PopupWindow : UserControl ? – There is no spoon May 30 '13 at 09:21
  • Great stuff, glad I was able to help. Please make sure to mark the answer! Have a look at [this](http://jamescrisp.org/2008/05/26/wpf-control-inheritance-with-generics/) for more information on WPF controls with generics. – Rob May 30 '13 at 09:23
  • The code behind for PopupWindowChild does not itself show any inheritance - I assume that the autogenerated code from the XAML would contain this instead? (in its partial class declaration)? – StayOnTarget Sep 05 '18 at 12:21