Sarah Vessels
Sarah Vessels

Reputation: 31640

WPF, share methods between two UserControl classes

I have two UserControl classes, RequiredFields and OptionalFields. I wanted to have a base class, Fields, that would inherit from UserControl and contain methods that both RequiredFields and OptionalFields would use. However, when I try to do that, I get the following error:

Partial declarations of 'MyNamespace.OptionalFields' must not specify different base classes

Here are my class definitions:

public partial class OptionalFields : Fields

public partial class RequiredFields : Fields

public abstract class Fields : UserControl

Even if I make Fields a partial class instead of abstract, or if I make it a regular non-abstract non-partial class, I get the same error.

Is what I'm wanting to do possible/reasonable? If not, what is the best way of sharing methods between UserControl instances?

Upvotes: 1

Views: 653

Answers (2)

chilltemp
chilltemp

Reputation: 8960

Do you have an OptionalFields.xaml? If so, it is automatically generating OptionalFields.g.cs which contains the C# code that the XAML represents. It contains a class that inherits from UserControl (or whatever the XAML's root element is).

Try changing the root element in the XAML file to Fields.

Upvotes: 2

chillitom
chillitom

Reputation: 25686

Do you need to updated the other side of the partial? I.e. is OptionalFields.Designer.cs inheriting from UserControl still?

Edit: sorry being too winforms, I of course mean OptionalFields.xaml

Upvotes: 0

Related Questions