100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
.NET

Data Templates

How DataTemplate lets WPF turn plain view-model objects into rich, reusable visual trees — the backbone of MVVM-driven UIs.

ControlsIntermediate10 min readJul 10, 2026
Analogies

What a DataTemplate Actually Does

A DataTemplate describes how a piece of data — usually a plain CLR object with no UI knowledge — should be rendered as a visual tree. When you set ContentControl.Content or ItemsControl.ItemsSource to arbitrary objects, WPF looks for an applicable DataTemplate and instantiates it once per item, binding its elements back to the data object's properties. This is what lets a ListBox bound to a List<Order> render each order as a card with a customer name, a total, and a status badge, instead of falling back to calling ToString() on each item.

🏏

Cricket analogy: A DataTemplate is like the standard broadcast graphics package that turns raw scorecard data (runs, balls, wickets) into the on-screen scoreboard overlay you see during a live match — same data, consistently formatted visual presentation.

Implicit Templates and DataType

When a DataTemplate is declared as a resource with a DataType set (e.g. DataType="{x:Type local:Order}") but no explicit x:Key, WPF treats it as implicit: it is automatically applied to any object of that CLR type rendered anywhere in that resource's scope, without you ever setting ItemTemplate manually. This is the mechanism behind polymorphic ItemsControls — a single ObservableCollection<object> containing both TextMessage and ImageMessage instances can render each with its own implicit DataTemplate simply because WPF matches on the runtime type at render time.

🏏

Cricket analogy: An implicit DataTemplate is like an umpire automatically applying the DRS review protocol whenever a specific type of appeal (LBW) comes up, without the fielding captain having to explicitly request that particular procedure each time.

DataTemplateSelector and HierarchicalDataTemplate

When type alone isn't enough to decide which template to use — for instance, rendering a Task differently depending on its Priority value rather than its type — you subclass DataTemplateSelector and override SelectTemplate(object item, DependencyObject container) to return the appropriate DataTemplate at runtime. For hierarchical data such as file-system trees or org charts, HierarchicalDataTemplate extends DataTemplate with an ItemsSource property that tells TreeView how to find each node's children, recursively applying itself (or a nested template) down the tree.

🏏

Cricket analogy: A DataTemplateSelector choosing between a 'Milestone' template and a 'Regular Over' template based on whether a batsman just reached a century is like a broadcast director cutting to a special graphic only when a specific in-match condition is met.

xml
<Window.Resources>
    <!-- Implicit template: applies automatically to any Order object -->
    <DataTemplate DataType="{x:Type local:Order}">
        <Border BorderBrush="#DDD" BorderThickness="1" CornerRadius="4" Padding="8" Margin="4">
            <StackPanel>
                <TextBlock Text="{Binding CustomerName}" FontWeight="Bold"/>
                <TextBlock Text="{Binding Total, StringFormat=C}"/>
                <TextBlock Text="{Binding Status}" Foreground="Gray" FontSize="11"/>
            </StackPanel>
        </Border>
    </DataTemplate>
</Window.Resources>

<ListBox ItemsSource="{Binding Orders}"/>

<!-- HierarchicalDataTemplate for a TreeView of categories -->
<TreeView ItemsSource="{Binding Categories}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding SubCategories}">
            <TextBlock Text="{Binding Name}"/>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

DataTemplates can be nested and can contain any layout panel, so a 'card' template with an image, title and metadata is just as valid as a single TextBlock. Because the template is instantiated per item, keep it lightweight for large ItemsControls — heavy visual trees per row (many nested Borders, Effects) can noticeably hurt scrolling performance in a virtualized ListBox.

An implicit DataTemplate only applies within the ResourceDictionary scope where it's declared (and scopes that inherit from it). Declaring it inside a single UserControl's resources means it will NOT automatically apply to the same data type rendered in a different window — move it to App.xaml's resources for truly global implicit templates.

  • A DataTemplate defines the visual tree used to render a plain data object, instantiated once per bound item.
  • Implicit templates (DataType set, no x:Key) auto-apply to any object of that type within their resource scope.
  • DataTemplateSelector lets you choose a template based on runtime conditions beyond just the object's type.
  • HierarchicalDataTemplate adds an ItemsSource property so TreeView can recursively render nested data.
  • Implicit template scope matters — declare in App.xaml resources for application-wide effect.
  • Heavy per-item visual trees in DataTemplates can hurt virtualization performance in large lists.

Practice what you learned

Was this page helpful?

Topics covered

#NET#WPFWindowsPresentationFoundationStudyNotes#MicrosoftTechnologies#DataTemplates#Data#Templates#DataTemplate#Actually#StudyNotes#SkillVeris