WPF Control Inheritance With Generics

Working in WPF is quite exciting - there’s a lot of new possibilities, especially with easy control composition, much improved binding and Expression Blend to make sexy interfaces. One of the things you’re likely to want to do though, when writing anything more than a toy application, is to have a base class for your UserControls or Windows, to share common functionality. It is also quite likely you will want to use generics in conjunction with control inheritance. With both the code behind, and the XAML, it’s not immediately obvious how to do generic inheritance. It is a bit fiddly to get going, and sometimes the errors are not helpful. Here’s a simple example that outlines how to bring it together.


The base control

namespace WpfGenericsDemo
{
    public class BaseUserControl<T> : UserControl where T : IPresenter
    {
        public BaseUserControl()
        {
            ... various configurations ...
        }

         ... Awesome functionality to share ...
    }
}


The child control code-behind

namespace WpfGenericsDemo
{
    public partial class ChildUserControl : BaseUserControl<ChildPresenter>
    {
        public ChildUserControl()
        {
            InitializeComponent();
        }

         ... More code ...
    }
}


The child control XAML

<WpfGenericsDemo:BaseUserControl x:Class="WpfGenericsDemo.ChildUserControl"
    x:TypeArguments="WpfGenericsDemo:ChildPresenter"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfGenericsDemo="clr-namespace:WpfGenericsDemo">

    ... The rest of your awesome XAML ...

</WpfGenericsDemo:BaseUserControl>


Notes

  • Your top level node is the parent class of the control you want to create (eg, BaseUserControl). You specify the control class you want to create with ‘x:class’ (eg, ChildUserControl)
  • ‘x:TypeArguments’ is the way you specify the generic type (eg, ChildPresenter)
  • You need to namespace your classes - eg, with ‘xmlns:WpfGenericsDemo’ which uses a clr-namespace style reference
  • Only your top level node can be genericised in XAML


Actions

Information

2 responses to “WPF Control Inheritance With Generics”

13 09 2008
Todd Beaulieu (06:23:44) :

I’ve found little on this topic. Thanks for the post. I must be thick, but I just can’t get this going. using VS 2008/3.5.

I have a ViewModel that I need to pass into a user control. No luck so far.

For starters, as soon as I add the generic construct, the InitializeComponent method disappears.

Assuming I can get this working, I was going to place an instance of each control, bound to various entities, on seperate PAGE objects that I am showing in a frame. It looks like I’d need to put them as root elements, though. I wonder if I can have a xaml file with just an instance of this UC with the x:TypeArguments specified and point a frame to it?

1 01 2009
Dan (01:10:37) :

You rock dude! Thats exactly what I was looking for. Thank you so much ;)

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>