You cannot change the .g.cs
file, in fact is says so right in the file. Also, it's unfortunate to use the term "custom control" as this means something specific and not what you are trying to do. But, the good news is that what you are trying to do is possible.
Derive from UserControl
:
public class FancyUserControl : UserControl
{
// Your added common functionality.
}
and then add a new UserControl
to your project using the normal mechanism, let's say UserControl1
. Then edit the UserControl.xaml
files as follows:
<local:FancyUserControl x:Class="SilverlightApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SilverlightApplication1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
</Grid>
</local:FancyUserControl>
paying special attention to the three lines with local
in them, adjusting to your application. Then edit the UserControl1.xaml.cs
file as follows:
public partial class UserControl1 : FancyUserControl
{
public UserControl1()
{
InitializeComponent();
}
}
and Visual Studio won't be quite happy yet but finally rebuild your project and all will be well.
The class UserControl1
is now derived from FancyUserControl
instead of UserControl
and you can begin adding your common functionality. To add more controls you will need to manually edit the XAML and code-behind once after initially adding each new control to the project.