views:

565

answers:

1

Hi guys, I have a set of UserControls that need to have a few similar properties. Thus I have defined an abstract subclass of UserControl that defines these properties and updated the .xaml.cs and .g.cs files to inherit from this base class. All compiles well and runs well. Great! But.... .g.cs files are generated and will be regenerated, so how do I tell Blend or Visual Studio to keep inheriting from my base class rather than UserControl?

Cheers

Nik

+4  A: 

You need to change the XAML a bit to prefix the UserControl declaration with a namespace:

<local:MyBaseControl x:Class="MyNameSpace.MyControl"
    xmlns:local="clr-namespace:MyNameSpace"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;

    <!-- Content -->
</local:MyBaseControl>

Where MyNameSpace is your namespace (duh!), MyBaseControl is your base class and MyControl is your control that inherits from MyBaseControl. The x:Class part doesn't need to be in the same namespace, I've just kept it the same for the example.

More info here and here.

Steven Robbins
One more question, though, because while this compiles fine, Expression Blend now gives me an 'Exception: Cannot create instance of "MyBaseControl"'. Is there any way to do this an still be able to use Blend?
niklassaers-vc
Steve: There is a typo in your code when you close the local tag. I don't have to powers to edit yet.
Eduardo Molteni
A note about a problem I encountered: If the compiler says that it can't find the tag in your local: namespace, it may be because the assembly containing the local namespace does not build.
Roman Plášil