I am having problems changing the DataTemplate that is used for the TreeViewItem when it is selected. Ideally, I would like each element to contain TextBlock
, and then when it was selected, it should contain TextBox
.
Here is what I have so far (I used this question as a starting point):
<Window>
<Window.Resources>
<HierarchicalDataTemplate x:Key="normal"
ItemsSource="{Binding Path=Children}">
<TextBlock Text="{Binding Path=Text}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="selected"
ItemsSource="{Binding Path=Children}">
<TextBox Text="{Binding Path=Text}" />
</HierarchicalDataTemplate>
<Style TargetType="{x:Type TreeViewItem}" x:Key="ContainerStyle">
<Setter Property="ItemTemplate" Value="{StaticResource normal}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="ItemTemplate" Value="{StaticResource selected}" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resource>
<Grid>
<TreeView ItemSource="{Binding Body}" ItemContainerStyle="{StaticResource ContainerStyle}" />
</Grid>
</Window>
It happens that there is only one node in the tree, and the text node is the name of the object type. It seems that the type associated with the node does not match the expected pattern, so it uses the default binding ToString()
instead of the property Text
, as I pointed out.
DataContext . , , HierarchicalDataTemplate
TreeView, .
, , , ItemTemplate
, TreeViewItem
- right - ?