Friday 29 December 2006

WPF/XAML - x:Type and nested classes

Well now I am getting started with XAML and WPF I am hoping to start blogging a bit more! So I was trying to reference a nested type with the {x:Type} markup extension and it wasn't working. Say you have a class as follows:

public class MyClass
{
    public class MyInnerClass
    {
    }
}

You want to reference the inner class (e.g. for a DataTemplate). The following XAML does not work

<DataTemplate DataType="{x:Type MyClass.MyInnerClass}">

What you need to do is:

<DataTemplate DataType="{x:Type MyClass+MyInnerClass}">

Thanks to Seb for the hint on that one!

4 comments:

Echilon said...

Cheers. MSDN seems to recommend against it but it works for me. :)

Neil Mosafi said...

They do? Where is that recommendation?

Anonymous said...

This works, but it seems that the designer in VS 2008 doesn't like it. I used it in DataTemplate and now I can't see the designer anymore!

Neil Mosafi said...

Erm that's a bit rubbish really! However I don't use the designer so haven't really had that problem. A lot of things seem to break the designer as far as I can tell!

Give it a go in vs2010, I'd be interested to see how it is there and the designer is supposed to be better

Cheers
Neil