Deriving from UserControl in Silverlight

silverlight, silverlight-2.0

Solution

If you include the namespace of your base class of UserControl, you can do it as long as you use the namespace. For example:

public abstract class MyBaseUserControl : UserControl
{
  // ...
} 

Then you have to use this class in the XAML (Note the my namespace and then using the new namespace as the root of the document):

<!-- Page.xaml -->
<my:BaseUserControl 
    x:Class="SilverlightApplication11.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:my="clr-namespace:SilverlightApplication11"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">

    </Grid>
</my:BaseUserControl>

This won't magically change the base class in the code-behind so change that code to your base class:

public partial class Page : BaseUserControl
{
  public Page()
  {
    InitializeComponent();
  }
}

Problem

In Silverlight 2 I have the following class declaration for a control: ``` public partial class ClassX : UserControl ``` I wish to replace UserControl with ClassXBase which derives from UserControl but I'm getting the reasonable error "Partial declarations of 'ClassX' must not specify different base classes" However, I'm unable to find the other partial class to replace its base class. Any idea where this other partial class is or how I do this?

Original source