2009-02-06 17 views
3

我在更改用於TreeViewItem的DataTemplate時會出現一些問題。理想情況下,我希望每個項目都包含TextBlock,然後在選擇時應包含TextBox當它被選中時更改TreeViewItem的模板

這裏是我到目前爲止(我用this question爲起點):

<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屬性。

我已經在代碼後面的文件中設置窗口的DataContext。我知道我的數據綁定是正確的,因爲如果我爲TreeView設置了一個HierarchicalDataTemplate,則數據顯示正確。

我認爲我的問題是我需要在TreeViewItem樣式中設置除ItemTemplate之外的其他屬性 - 我是使用正確的屬性,還是應該設置其他屬性?

+0

錯字在XAML:「HierarchialDataTemplate」應爲「HierarchicalDataTemplate 」。 – 2016-02-26 19:48:35

+0

@MarkMiller固定 - 謝謝! – Andy 2016-02-26 20:31:25

回答

2

它實際上就是你需要的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 
相關問題