Wpf user control base class problem

inheritanceuser-controlswpf

I am new to WPF and have created a WPF User Control Library

I added a Base class that looks like this

public class TControl : UserControl
{
}

and want all of my controls to inherit from it.

I have a Control called Notification which looks like

public partial class Notification : TControl
{
    public Notification()
    {
        InitializeComponent();
    }

Works fine except when ever i recompile the hidden partial class where InitializeComponent() is defined gets regenerated and inherits from System.Windows.Controls.UserControl

this gives me an

Partial declarations of 'Twac.RealBoss.UserControls.Notification' must not specify different base classes

error,

is there anyway to force the generated class to inherit from my base class?

Best Answer

Your XAML file probably has:

<UserControl x:Class="YourNamespace.Notification" .... >

Try changing this to:

<Whatever:TControl x:Class="YourNamespace.Notification" xmlns:Whatever="clr-namespace:YourNamespace" />

The error you are getting is because the use of UserControl in the XAML tells the compiler to produce a partial class inheriting from UserControl, instead of inheriting from your class.

Related Topic