views:

330

answers:

1

How do I derive a new class from DataTemplate and then use it in place of DataTemplate?

For example, in C#:

public class DerivedDataTemplate : DataTemplate
{
}

Then in XAML:

<local: DerivedDataTemplate DataType="{x:Type local:SomeType}">
    <Grid>
        ... UI here ...
    </Grid>
</local:DerivedDataTemplate>

When I try to use my derived data template class the follow exception is generated:

System.Windows.Markup.XamlParseException was unhandled Message="'Grid' object cannot be added to 'DerivedDataTemplate'. Object of type 'System.Windows.Controls.Grid' cannot be converted to type 'System.Windows.FrameworkElementFactory'.

Does anyone know how to work around this exception and successfully use a class derived from DataTemplate?

A: 

Hi Ashley,

It worked for me (although I did not create a local type). I copied your code and had to remove the space between local: and DerivedDataTemplate in the XAML declaration. In Blend 3 it wouldn't even run for me, I'm not sure about Blend 2.

Joel Cochran
The space was just a typo when putting it on SO, my real code didn't have that error. So when you say it worked for you what exactly do you mean? In the next sentence you seem to say that it didn't work for you!? I am using Visual Studio 2008 Express.
Ashley Davis
Sorry, I wasn't clear: your code worked for me without the space. *with* the space it would not even run in Blend 3.
Joel Cochran