Implementing your own base class for user controls in Silverlight 2
The objective is to create your own base class for user controls to implement application related features and at the same time also use the features provided by the Visual Studio (i.e. auto generate a partial class that initialize all UI elements). The process can be described best with three projects:
For the sake of simplicity I created a simple UserControlBase class extending from the UserControl class. This class can contain the common methods and properties as needed for your application. Here I have added some dummy methods and properties.
namespace BaseLibrary
{
public class UserControlBase : UserControl
{
public int Id { get; set; }
public void DoSomeThing()
{
//…
}
}
}
Then, lets create a TestControl class and a TestControl.xaml in the class library where we like the have the custom controls:
namespace CustomControls
{
public partial class TestControl : UserControlBase
{
public TestControl()
{
InitializeComponent();
}
}
}
Now, here is the trick. Look closely to the xaml. Instead of regular UserControl, we used our own base class. To do so, we also have to include the namespace.
UserControlBase x:Class=”MyControls.TestControl”
xmlns=”http://schemas.microsoft.com/client/2007″
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
xmlns:bl=”clr-namespace:BaseLibrary;assembly=BaseLibrary”
Width=”150″ Height=”50″>
<TextBlock Text=”I am a test control”/>
</bl:UserControlBase>
In this way, Visual Studio also generates the partial class properly. But there is one side effect: the Visual Studio will not be able to show you the UI preview in designer. I haven’t found any work around yet.
Update:
I forgot to add the reference in AsseblyInfo.cs file of BaseLibrary project. Once you add the following reference the Visual Studio will render the UI properly. Thanks to Michael for pointout the issue.
[assembly: XmlnsDefinition("http://schemas.microsoft.com/client/2007", "BaseLibrary")]
I have also updated the sample code.
Download Sample Code

Just wanted to add that Expression Blend also does not display the user control correctly.
This caused me a lot of headaches with Beta 1, so I too am VERY disappointed that this doesn’t work in Beta 2!!
You should add the namespace mapping in AssemblyInfo.cs.
I wrote about that in my blog long time back.
http://michaelsync.net/2008/03/24/silverlight-2-beta1-user-control-inheritance
Hi Michael,
Thanks a lot for point out the problem. I forgot to add the reference. Its updated now.
Excellent post !!! Is just what I was looking for, extend an usercontrol with some base functionallity without having to bother about having an intermediate XAML and do nasty tricks that could not work in the next Beta/RC version.
Tried on SL 2, Beta 2 and working perfect.
Nice one Shahed, thanks
Great post! Thank you for spreading this knowledge. I would just like to add minor detail in case someone else experiences the same issue as I did: your base class should derive from a class not a UserControl. Thanks!
OK, good info. I’ve another problem with the same technic and generics.
rapidly, here’s my code :
namespace MyApp
{
public class BasePage : UserControl {…}
public class BaseEditPage: BasePage where T: WCFService.EntityObject, new() {…}
public class SomethingEdit : BaseEditPage {…}
}
I’ve no error with this code
BUT… my xaml don”t compile:
some supplement info:
the WCFService represent the service WCF that is link to my project and SomethingXXX is class with [DataContract] attribute.
x:TypeArgument is about to specified the Generic Class in the UserControl xaml !
this don’t run cause the file SomethingEdit.g.cs (the hidden file partial class) don’t generate with the normal generic specification like this: SomethingEdit !!
any help will be very great !!!
to read you,
Patrice