user203687
user203687

Reputation: 7237

Template vs. ControlTemplate vs. DataTemplate

We have Templates, Control Templates and Data Templates for WPF controls. I don't know if there exists any more types (please let me know if any). But, when to use what?

Upvotes: 1

Views: 1566

Answers (3)

Matt West
Matt West

Reputation: 2944

As far as when to use what - you can usually tell by what you are overriding. It will be of either the type ControlTemplate or DataTemplate.

Upvotes: 0

Pavlo Glazkov
Pavlo Glazkov

Reputation: 20746

Look at the FrameworkTemplate derived types:

  • ControlTemplate - Specifies the visual structure and behavioral aspects of a Control that can be shared across multiple instances of the control.;
  • DataTemplate - Describes the visual structure of a data object;
  • ItemsPanelTemplate - Specifies the panel that the ItemsPresenter creates for the layout of the items of an ItemsControl.

P.S.: As you can see there is no just "Template" :)

Upvotes: 3

kenwarner
kenwarner

Reputation: 29120

HierarchicalDataTemplate

<UserControl.Resources>
    <HierarchicalDataTemplate DataType="{x:Type src:League}" ItemsSource="{Binding Path=Divisions}">
        <TextBlock Text="{Binding Path=Name}"/>
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="{x:Type src:Division}" ItemsSource = "{Binding Path=Teams}">
        <TextBlock Text="{Binding Path=Name}"/>
    </HierarchicalDataTemplate>

    <DataTemplate DataType="{x:Type src:Team}">
        <TextBlock Text="{Binding Path=Name}"/>
    </DataTemplate>
</UserControl.Resources>

Upvotes: 1

Related Questions