Inheritance with WPF DataTemplate

Feb 28, 2013 at 9:17 AM
Hello,
I use UpdateControls in my WPF application.
And I would like to ask about inherite object in WPF DataTemplate.
Suppose there is
BaseObject,
and inherited: ObjectA : BaseObject and ObjectB : BaseObject.

I can switch views for that objects using construction which I found on this website:
In Resources:
<u:WrappedType x:Key="typeObjectA" Type={x:Type ObjectA}"/>
<u:WrappedType x:Key="typeObjectB" Type={x:Type ObjectB}"/>

<DataTemplate DataType="{StaticResource typeObjectA}">
<!-- View for ObjectA -->
</DataTemplate>
<DataTemplate DataType="{StaticResource typeObjectB}">
<!-- View for ObjectB -->
</DataTemplate>
Then for display one:
<ContentControl Content="{Binding}"/>
But when I tried to display in the same way same view for inherited objects (DataType for "typeBaseObject") it fails. I guess that wrapping object for UpdateControls loses information about inheritance or something like that. Is there any way to make a views upon the base type?

Martin
Coordinator
Feb 28, 2013 at 3:45 PM
The wrapper uses the most derived class. It does not match against the base class.

So I would recommend that you create a User Control for the view. Then you can define both Data Templates to use the same User Control.

Hope this helps!
Mar 1, 2013 at 11:32 AM
Edited Mar 1, 2013 at 11:33 AM
I think it is good solution. Thank you.