I have a TreeView
with 3 levels in my WPF application.
Level 1 and level 2 have their own HierarchicalDataTemplate
. Level 3 has its own DataTemplate
.
Here is the XAML code:
<Window.Resources>
<HierarchicalDataTemplate x:Key="Level1Template"
ItemsSource="{Binding Value}"
ItemTemplate="{StaticResource Level2RedTemplate}">
<Border Background="Green">
<TextBlock Text="Level1"/>
</Border>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="Level2RedTemplate"
ItemsSource="{Binding Value}"
ItemTemplate="{StaticResource Level3Template}">
<Border Background="Red">
<TextBlock Text="Level2"/>
</Border>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="Level2YellowTemplate"
ItemsSource="{Binding Value}"
ItemTemplate="{StaticResource Level3Template}">
<Border Background="Yellow">
<TextBlock Text="Level2"/>
</HierarchicalDataTemplate>
<DataTemplate x:Key="Level3Template">
<Border Background="LightBlue">
<TextBlock Text="Level3"/>
</Border>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Row="0"
Content="Change Template"
Click="ChangeTemplate" />
<TreeView Grid.Row="1"
Name="tv"
ItemsSource="{Binding Items}"
ItemTemplate="{StaticResource Level1Template}"/>
</Grid>
It works perfectly until here. The problem starts, when I want to change the DataTemplate
of Level2 items by pressing a button in my window (see figure below).
I tried to assign a new HierarchicalDataTemplate
to my TreeView
from code behind (in the event handler of the button) like this:
Private Sub ChangeTemplate()
CType(tv.ItemTemplate, HierarchicalDataTemplate).ItemTemplate = CType(Me.FindResource("Level2YellowTemplate"), HierarchicalDataTemplate)
End Sub
This is not possible, as I get an error, that "as soon as a Template-Object is used (sealed), it cannot be changed."
Is there any way, that I can change this template?
DataTemplateSelector
. You will be able to switch from a template to another during runtime. – P.MantheDataTemplateSelector
selects theDataTemplate
based on some properties of my data, is that right? – YvonnilaItemsSource
(which has the same type as the oldItemsSource
), and I want a new template for this new source. – Yvonnila