4

我在更改用于 TreeViewItem 的 DataTemplate 时遇到了一些问题。理想情况下,我希望每个项目都包含 a TextBlock,然后在选择时应该包含 a TextBox

到目前为止,这是我所拥有的(我以这个问题为起点):

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

发生的情况是树中只有一个节点,节点的文本是对象的类型名称。听起来绑定到节点的类型不是模板所期望的,所以它使用默认ToString()绑定而不是Text我指定的属性。

我已经在代码隐藏文件中设置了 Window 的 DataContext。我知道我的数据绑定是正确的,因为如果我HierarchicalDataTemplate为 TreeView 设置一个,数据就会正确显示。

我认为我的问题是我需要设置样式以外的属性ItemTemplate-TreeViewItem我使用的是正确的属性,还是应该设置其他属性?

4

1 回答 1

3

它实际上是您需要的 HeaderTemplate - 这就是控制节点本身样式的内容。就这样有一个完整的样本,这对我有用:

<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
于 2009-02-06T15:50:47.067 回答