0

I code in wpf c#

partial class A : userControl
{
}

partial class B : userControl 
{
}

But i have to make an inheritance between them

partial class A : userControl
{
}

partial class B : A
{
}

But when i do that i have an error saying :

Partial declarations of B must not specify different base classes
Markus Safar
  • 6,324
  • 5
  • 28
  • 44
Loli
  • 1
  • 1
  • 1
    Look here: [Inheriting from a UserControl in WPF](https://stackoverflow.com/questions/269106/inheriting-from-a-usercontrol-in-wpf). In short, you cannot inherit from a user control that has a XAML file. You need to create a plain C# class as the base class – Mohammad Dehghan Apr 10 '20 at 19:44
  • @Clemenes This post by itself is useful to newer WPF programmers who are trying the same approach as what OP described. The first thing comes to a programmer's mind is to change the base class. After changing the base class, he encounters this error. And my answer explains the reason for this error. The question should be reopnned. – Mohammad Dehghan Apr 10 '20 at 20:23

1 Answers1

-1

You cannot inherit from a user control that has a XAML file. You need to create a plain C# class as the base class:

// File: A.cs
public class A : UserControl
{
    public A()
    {
        // create your controls programmatically
    }
}

Then in your sub class's XAML file:

// File: B.xaml
<local:A x:Class="WpfApp1.B"
             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:local="clr-namespace:WpfApp1"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>

    </Grid>
</local:A>

(replace WpfApp1 with your namespace, obviously)

Note that the root tag name is local:A instead of UserControl. This identifies your control's base class. The x:Class=... part specifies the class name of the inherited user control.

Mohammad Dehghan
  • 17,853
  • 3
  • 55
  • 72