它实际上是您需要的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