Class DataTemplate
Builds a visual tree from markup content for an arbitrary data object - the templating mechanism ItemsControl uses to turn each bound item into a realized element.
public class DataTemplate
- Inheritance
-
DataTemplate
- Derived
- Inherited Members
- Extension Methods
Remarks
Like ControlTemplate, content is captured once when the DataTemplate itself is loaded (see MarkupLoader's own DataTemplate special case) and instantiated fresh - via Build(object) - every time it's needed. Two calls to Build(object), even for the same data item, each get their own independent visual subtree.
Unlike ControlTemplate, this has no TargetType (it works against any data object's
runtime type) and its content can't use {TemplateBinding} (there's no templated control to bind back
to) - instead, Build(object) sets the built tree's root DataContext to
the data item, and ordinary {Binding} inside the template resolves against that.
Constructors
DataTemplate()
Initializes a new instance of the DataTemplate class with no content. Content is supplied by MarkupLoader when the template is loaded from markup.
public DataTemplate()
Methods
Build(object)
Builds a fresh visual tree from this template's content, for dataItem.
public UIElement Build(object dataItem)
Parameters
dataItemobjectThe data object the built tree's root DataContext is set to - what every
{Binding}inside the template resolves against.
Returns
- UIElement
The freshly built root element - never shared with any other call, even for the same data item.
Exceptions
- InvalidOperationException
This template was never given any content.
- MarkupException
The content is malformed, or breaks a rule of the markup language.