Let me explain you my situation.
I have a base class called Shape, and several concrete classes like Triangle, Square, etc.
I have several data templates.
I’m building just one object. So I wouldn’t use an ItemControl control, I would like to use a normal panel like the grid, and show the respective data template (in DataContext has the concrete item)..
The only way to do this is using an ItemsControl? Or there’s another way.. because I’m just using one item and not a collection and display the correct template.
DataTemplatesare used in much more than justItemsControlsThey are used to tell WPF how to draw any object in the Visual Tree. For example, if you stick a
Userclass object in theVisualTree, aDataTemplatecan be used to tell WPF how to draw thatUserobjectThey are most frequently used in controls with an
ItemsSourceorContentproperties, because those are the most common way of inserting data objects into theVisualTree.In your specific case where you only want to insert one data item into the VisualTree, I would suggest a
ContentControlTo tell WPF how to draw
MyDataObjectyou can either use theContentTemplateproperty and set it to aDataTemplateor define an implicit
DataTemplatethat tells WPF to draw any object of a specific type using a specific template.