We already saw one example of defining hierarchical data template here. Now we are going to extend this example and define control template too. The simplest way to define control template is using style. It would be something like this in resource section.
- <Style x:Key="menuItem" TargetType="{x:Type MenuItem}">
Here important this is that we have to define the target type. If we don’t define the target type, then we can’t set the template property inside the style and we will get the following error.
- error MC4003: Cannot resolve the Style Property 'Template'. Verify that the owning type is the Style's TargetType, or use Class.Property syntax to specify the Property.
Here is simple style to define the control template for menu item.
- <Style x:Key="menuItem" TargetType="{x:Type MenuItem}">
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type MenuItem}">
- <Border Margin="3" Name="Border" Background="AliceBlue"
- BorderThickness="1" CornerRadius="5" BorderBrush="Black">
- <ContentPresenter Margin="3" Name="HeaderHost" ContentSource="Header"/>
- </Border>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
Now we are going to use this style. We can use it inside the data template, which we define as a part of defining hierarchical data template. Here is a piece of code to use this style.
- <Menu.ItemTemplate>
- <HierarchicalDataTemplate ItemsSource="{Binding Path=Cities}">
- <TextBlock Margin="1" Text="{Binding Name}"/>
- <HierarchicalDataTemplate.ItemTemplate>
- <DataTemplate>
- <MenuItem Style="{StaticResource menuItem}" Margin="1" Header="{Binding Name}"/>
- </DataTemplate>
- </HierarchicalDataTemplate.ItemTemplate>
- </HierarchicalDataTemplate>
- </Menu.ItemTemplate>
Here is complete XAML code of this program.
- <Window x:Class="WpfMenu.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="MainWindow" Height="350" Width="525">
- <Window.Resources>
- <Style x:Key="menuItem" TargetType="{x:Type MenuItem}">
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type MenuItem}">
- <Border Margin="3" Name="Border" Background="AliceBlue"
- BorderThickness="1" CornerRadius="5" BorderBrush="Black">
- <ContentPresenter Margin="3" Name="HeaderHost" ContentSource="Header"/>
- </Border>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- </Window.Resources>
- <DockPanel DataContext="{Binding}">
- <Menu DockPanel.Dock="Top" ItemsSource="{Binding}">
- <Menu.ItemTemplate>
- <HierarchicalDataTemplate ItemsSource="{Binding Path=Cities}">
- <TextBlock Margin="1" Text="{Binding Name}"/>
- <HierarchicalDataTemplate.ItemTemplate>
- <DataTemplate>
- <MenuItem Style="{StaticResource menuItem}" Margin="1" Header="{Binding Name}"/>
- </DataTemplate>
- </HierarchicalDataTemplate.ItemTemplate>
- </HierarchicalDataTemplate>
- </Menu.ItemTemplate>
- </Menu>
- <TextBlock/>
- </DockPanel>
- </Window>
And here is complete C# code of the program.
- <Window x:Class="WpfMenu.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="MainWindow" Height="300" Width="400">
- <Window.Resources>
- <Style x:Key="menuItem" TargetType="{x:Type MenuItem}">
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type MenuItem}">
- <Border Margin="3" Name="Border" Background="AliceBlue"
- BorderThickness="1" CornerRadius="5" BorderBrush="Black">
- <ContentPresenter Margin="3" Name="HeaderHost" ContentSource="Header"/>
- </Border>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- </Window.Resources>
- <DockPanel DataContext="{Binding}">
- <Menu DockPanel.Dock="Top" ItemsSource="{Binding}">
- <Menu.ItemTemplate>
- <HierarchicalDataTemplate ItemsSource="{Binding Path=Cities}">
- <TextBlock Margin="1" Text="{Binding Name}"/>
- <HierarchicalDataTemplate.ItemTemplate>
- <DataTemplate>
- <MenuItem Style="{StaticResource menuItem}" Margin="1" Header="{Binding Name}"/>
- </DataTemplate>
- </HierarchicalDataTemplate.ItemTemplate>
- </HierarchicalDataTemplate>
- </Menu.ItemTemplate>
- </Menu>
- <TextBlock/>
- </DockPanel>
- </Window>
This is the output of the program.

Where the C# code? Tq
By: sa on March 24, 2011
at 1:05 am
I defined the control template completely in XAML and used the same C# code which i used here
http://zamjad.wordpress.com/2009/12/18/using-hierarchical-data-template-with-menu-item/
By: zamjad on March 24, 2011
at 7:42 am