You cannot modify the .g.cs file, it is actually said so correctly in the file. Also, unfortunately, use the term "user control" because it means something specific, not what you are trying to do. But the good news is that what you are trying to do is possible.
Output from UserControl :
public class FancyUserControl : UserControl {
and then add a new UserControl to your project using the usual mechanism, 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 particular attention to the three lines with local in them, adapting to your application. Then edit the UserControl1.xaml.cs file as follows:
public partial class UserControl1 : FancyUserControl { public UserControl1() { InitializeComponent(); } }
and Visual Studio will not be happy yet, but finally rebuild your project and everything will be fine.
The UserControl1 class is now derived from FancyUserControl instead of UserControl , and you can start adding your common functions. To add additional controls, you need to manually edit the XAML and code after once after the initial addition of each new control to the project.
source share