Changing the TreeViewItem Template When It Is Selected

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 - ?

+3
1

HeaderTemplate - , node. , , , :

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<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="HeaderTemplate"
                Value="{StaticResource normal}" />
        <Style.Triggers>
            <Trigger Property="IsSelected"
                     Value="True">
                <Setter Property="HeaderTemplate"
                        Value="{StaticResource selected}" />
            </Trigger>
        </Style.Triggers>
    </Style>
    </Window.Resources>
    <Grid>
        <TreeView x:Name="_Tree" ItemContainerStyle="{StaticResource ContainerStyle}"/>
    </Grid>
</Window>

.. , :

Imports System.Collections.ObjectModel

Class Window1

    Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded

        Dim Root As New Node
        Root.Text = "Root"

        Dim Child As New Node
        Child.Text = "Child"
        Root.Children.Add(Child)

        Dim Nodes As New Collection(Of Node)
        Nodes.Add(Root)
        _tree.itemssource = Nodes

    End Sub

End Class

Public Class Node

    Private _Text As String
    Public Property Text() As String
        Get
            Return _Text
        End Get
        Set(ByVal Value As String)
            _Text = Value
        End Set
    End Property

    Private _Children As New Collection(Of Node)
    Public Property Children() As Collection(of node)
        Get
            Return _Children
        End Get
        Set(ByVal Value As Collection(of node))
            _Children = Value
        End Set
    End Property

End Class
+2

Source: https://habr.com/ru/post/1702797/


All Articles