Search code examples
c#.netwpfpixelsense

Using polymorphism in (Surface)UserControl?


My (Surface)UserControls share a big amount of code. So I'd like to define an abstract super class where I can define this common code and behaviour.

But how do I define an abstract super class for a (Surface)UserControl? I tried to just subclass the code-behind file of a SurfaceUserControl, but then I get the following error:

Partial declarations of 'Prototype_Concept_2.views.DependencyExpander' must not specify different base classes  C:\Users\Roflcoptr\Documents\Visual Studio 2008\Projects\Prototype_Concept_2\Prototype_Concept_2\views\DependencyExpander.xaml.cs

Solution

  • Deriving from SurfaceUserControl (or SurfaceControl or SurfaceContentControl or SurfaceItemsControl as appropriate) is definitely a good thing because it will make it easier for consumers of your control to work with Surface events on them. You are right though that this results in a lot of duplication of code.

    When we were building the Surface SDK, a lot of automated code generation was employed to mitigate this issue. If you use Reflector to look at how the Surface controls are implemented, you will see that there is very little logic built into the controls themselve are a few helper classes which contain a ton of logic that gets leveraged by each of the individual controls.