2010-10-19 25 views
2

長篇博客的道歉 - 閱讀關於此的幾個主題,但仍然很難實現。基本上,我有對象,定義爲一個集合:WPF Treeview - 綁定到具有不同深度的集合,並且樣式不同

public class LibData 
{ 
    public string Name { get; set; } 
    ObservableCollection<LibObject> _list; 
    public ObservableCollection<LibObject> List { get { return _list; } } 

    public LibData(string name, LibDataType type) 
    { 
     this.Name = name; 
     _list = new ObservableCollection<LibObject>(); 
    } 
} 

和對象:

public class LibObject 
{ 
    public string Name { get; set; } 

    public LibObject(string name) 
    { 
     this.Name = name; 
    } 
} 

我的主要問題是在XAML,而這種造型樹視圖。我需要有一個「根」項的特定樣式,以及「葉」的特定樣式。事情是,綁定列表中的一個項目是「Root-> Leaf」,另一個是「Root-> Child-> Leaf」。 我嘗試這樣做:

<TreeView x:Name="myTree" ItemsSource="{x:Static local:myDataList}"> 
    <TreeView.ItemTemplate> 
     <HierarchicalDataTemplate ItemsSource="{Binding Path=List}" > 
      <Grid> 
       <StackPanel Orientation="Horizontal"> 
         <TextBlock Text="{Binding Path=Name}" /> 
         <CheckBox IsChecked="True" Content="HeaderCheckbox"/> 
       </StackPanel> 
      </Grid> 
     <HierarchicalDataTemplate.ItemTemplate > 
       <DataTemplate> 
        <Grid> 
      <StackPanel Orientation="Horizontal"> 
       <CheckBox IsChecked="True" Content="LeafCheckbox" /> 
       <TextBlock Text="{Binding Path=Name}"/> 
      </StackPanel> 
        </Grid> 
       </DataTemplate> 
      </HierarchicalDataTemplate.ItemTemplate> 
     </HierarchicalDataTemplate> 
    </TreeView.ItemTemplate> 

這顯然能正常工作的「根 - >葉」項目,而不是「根 - 孩子 - 葉」。 XAML實現似乎更像是一種「硬編碼」解決方案,我知道項目佈局始終是「Root-> Leaf」 - 我如何使這種動態? 同樣,我已經看到了不同級別的解決方案(包括使用轉換器),但是我遇到的問題是我需要特定的樣式來創建根和葉,並且沒有任何級別。 我想知道如果我看着這個完全錯誤的...

回答

6

簡單的方法來做到這一點。將DataTemplates從TreeView中拉出並放入資源部分。爲每個DataTemplate指定DataType屬性,但不要包含密鑰。 TreeView中的ItemTemplateSelector(類型爲DataTemplateSelector的屬性)將使用DataTemplate適合正確的項目類型的功能。

爲類型Root和Child創建HierarchicalDataTemplate,爲Leaf類型創建DataTemplate。

事情是這樣的:

<Window.Resources> 
    <ResourceDictionary> 

     <DataTemplate DataType="{x:Type local:Leaf}"> 
      <Grid> 
       <StackPanel Orientation="Horizontal"> 
        <CheckBox IsChecked="True" Content="LeafCheckbox" /> 
        <TextBlock Text="{Binding Path=SomeValue}"/> 
       </StackPanel> 
      </Grid> 
     </DataTemplate> 

     <HierarchicalDataTemplate DataType="{x:Type local:Child}" 
            ItemsSource="{Binding Children}"> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="Child " /> 
       <TextBlock Text="{Binding Path=SomeValue}" /> 
      </StackPanel> 
     </HierarchicalDataTemplate> 

     <HierarchicalDataTemplate DataType="{x:Type local:Root}" 
            ItemsSource="{Binding Children}"> 
      <Grid> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="Root " /> 
        <TextBlock Text="{Binding Path=SomeValue}" /> 
       </StackPanel> 
      </Grid> 
     </HierarchicalDataTemplate> 

    </ResourceDictionary> 
</Window.Resources> 

<Grid> 
    <TreeView x:Name="myTree" ItemsSource="{Binding}" /> 
</Grid> 
+0

是取得模板出來的樹狀資源關鍵的一部分?我沒有提到這一點,但我在一個嚮導中使用它,每個「窗口」實際上是一個PageFunction - 據我瞭解,沒有資源。 – user350213 2010-10-19 22:20:09

+0

是的,有必要將DataTemplates從TreeView中取出。您希望這樣做,因爲您不想直接指定ItemTemplate,因爲在這種情況下,模板應根據項目類型進行更改。在我匆忙的例子中,我把DataTemplates放在了Window.Resources中,但是TreeView.Resources也是如此。 – mbursill 2010-10-19 22:46:14

+0

哦,我明白了 - 所以把它放在子句中就可以了。涼。我會試一試。謝謝。 – user350213 2010-10-19 23:01:27

相關問題