Reputation: 5813
I Would like to create several WPF UserControl
classes with some common functionality. For this reason I would like to derive the classes from a new base class that in turns derives from UserControl
.
My problem is that my C# class is defined partial
, and the automatically generated other part derives
from UserControl
and not from my new base class. Can I make Visual Studio derive the partial class from my base class instead of UserControl
?
Upvotes: 2
Views: 2180
Reputation: 15569
Sure - create a class that serves as the base call for your user controls:
public class BaseUserControl : UserControl
{
//TODO: Add your common functionality
}
Next, create a user control, and change the root declaration to reference your base class:
<controls:BaseUserControl x:Class="WpfApplication2.UserControl1"
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:controls="clr-namespace:WpfApplication2"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
</Grid>
</controls:BaseUserControl>
And change the partial class to match as well:
public partial class UserControl1 : BaseUserControl
{
public UserControl1()
{
InitializeComponent();
}
}
Upvotes: 8