I am creating a series of UserControls that all have some similar business logic. So I tried to implement this common business logic in an abstract base class that all of these user controls would inherit from. The inheritance chain looks something like this:
System.Windows.Controls.UserControl <-- MyControlBase (abstract)<--MyControl1, MyControl2, etc.
I've created the MyControl1, MyControl2 etc. classes as User Controls in Visual Studio. I've set the classes to inherit from MyControlBase instead of System.Windows.Controls.UserControl. It works great... until I make a change in the XAML and re-save. Visual Studio 2008 then re-assigns the generated class to inherit from System.Windows.Controls.UserControl.
The code example of the classes and XAML are as follows:
// MyControlBase.cs:
public abstract class MyControlBase.Windows.Controls.UserControl
{
public abstract void DoOperation1();
}
// MyControlBase.xaml: none
// MyControl1.cs:
public partial class MyControl1: MyControlBase
{
public MyControl1()
{
InitializeComponent();
}
public override void DoOperation1()
{
return;
}
}
// MyControl1.xaml:
<UserControl x:Class="Test.Controls.BarChart"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Name="LayoutRoot" Background="Blue">
</Grid>
</UserControl>
// mycontrol1.g.cs (generated by Visual Studio) orginally
public partial class MyControl1: System.Windows.Controls.UserControl
{
internal System.Windows.Controls.Grid LayoutRoot;
// InitilizeComponent() etc
}
// mycontrol1.g.cs (generated by Visual Studio) modified by me
public partial class MyControl1: MyControlBase
{
internal System.Windows.Controls.Grid LayoutRoot;
// InitilizeComponent() etc
}
Whenever editing MyControl1.xaml in the Visual Studio 2008 editor after saving it the modified mycontrol1.g.cs keeps reverting itself to the original one (namingly: inheriting again from System.Windows.Controls.UserControl). The compile then fails because the two partial classes of MyControl1 are not inheriting from the same parent: the VS generated one inherits from System.Windows.Controls.UserControl, my code behind from MyControlBase.
Is there a way to work around this issue? I would like to have the ability to inherit from an abstract class and assume there is a way for Visual Studio to let me, I just don't see what I'm missing. Or is the concept of a UserControl inheriting from another class than System.Windows.Controls.UserControl a bad concept?