2012-05-08 116 views
2

我有一個數據庫,看起來像:複雜的Silverlight TreeView,是否可以嵌套層次結構?

Locations   [rootlevel] 
    Inspections [level1] 
    Areas   [level1] 
     Inspections [level2] 

因此,每個位置能有零個或多個檢查和零個或多個區域,區域具有零次或多個檢查。 Inspections的記錄都有一個LocationID!= null和AreaID = null或!= null來獲取這個層次結構。

我想獲取表中每個項目的所有名稱作爲導航樹視圖中。到目前爲止,我可以得到無論是

位置 - >領域 - >檢查或

位置 - >檢查

我似乎無法得到樹狀層次結構,以顯示我需要什麼。可能嗎?我曾嘗試使用嵌套樹視圖作爲層次結構中的項目來顯示我想要的內容,但它無法正常工作。對於位置

XAML代碼 - >領域 - >檢查

<!--NAVIGATION TREE HIERARCHICAL TEMPLATE--> 
    <common:HierarchicalDataTemplate x:Key="AssetManager" ItemsSource="{Binding Path=Areas}"> 

     <!--START OF AREA OPTIONS TEMPLATE--> 
     <common:HierarchicalDataTemplate.ItemTemplate> 
      <common:HierarchicalDataTemplate ItemsSource="{Binding Path=Inspections}"> 

       <!--START OF INSPECTION OPTIONS TEMPLATE--> 
       <common:HierarchicalDataTemplate.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Horizontal"> 
          <Image Source="Assets/Resources/ImageResources/SearchICON2.png" Height="20" Width="20" /> 
          <TextBlock Margin="0,0,0,0" Text="{Binding Path=Name}"/> 
         </StackPanel> 
        </DataTemplate> 
       </common:HierarchicalDataTemplate.ItemTemplate> 
       <!--END OF INSPECTION OPTIONS TEMPLATE--> 

       <StackPanel Orientation="Horizontal"> 
        <Image Source="Assets/Resources/ImageResources/ManufacturingICON.png" Width="20" Height="20"/> 
        <TextBlock Margin="0,0,0,0" Text="{Binding Path=Name}"/> 
       </StackPanel> 
      </common:HierarchicalDataTemplate> 
     </common:HierarchicalDataTemplate.ItemTemplate> 
     <!--END OF AREA OPTIONS TEMPLATE--> 


      <StackPanel Orientation="Horizontal"> 
        <Image Source="Assets/Resources/ImageResources/ManufacturingICON.png" Width="20" Height="20"/> 
        <TextBlock Margin="0,0,0,0" Text="{Binding Path=Name}"/>      
      </StackPanel>  
    </common:HierarchicalDataTemplate> 
    <!--END OF NAVIGATION TEMPLATE--> 

XAML的位置 - >檢查

<!--NAVIGATION TREE HIERARCHICAL TEMPLATE--> 
    <common:HierarchicalDataTemplate x:Key="AssetManager" ItemsSource="{Binding Path=Inspections}"> 
      <StackPanel Orientation="Horizontal"> 
        <Image Source="Assets/Resources/ImageResources/ManufacturingICON.png" Width="20" Height="20"/> 
        <TextBlock Margin="0,0,0,0" Text="{Binding Path=Name}"/> 
       </StackPanel> 
      </common:HierarchicalDataTemplate> 
     </common:HierarchicalDataTemplate.ItemTemplate> 
     <!--END OF TEMPLATE--> 

XAML嵌套樹狀

<!--NAVIGATION TREE HIERARCHICAL TEMPLATE--> 
    <common:HierarchicalDataTemplate x:Key="AssetManager" ItemsSource="{Binding Path=Areas}"> 

     <!--START OF AREA OPTIONS TEMPLATE--> 
     <common:HierarchicalDataTemplate.ItemTemplate> 
      <common:HierarchicalDataTemplate ItemsSource="{Binding Path=Inspections}"> 

       <!--START OF INSPECTION OPTIONS TEMPLATE--> 
       <common:HierarchicalDataTemplate.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Horizontal"> 
          <Image Source="Assets/Resources/ImageResources/SearchICON2.png" Height="20" Width="20" /> 
          <TextBlock Margin="0,0,0,0" Text="{Binding Path=Name}"/> 
         </StackPanel> 
        </DataTemplate> 
       </common:HierarchicalDataTemplate.ItemTemplate> 
       <!--END OF INSPECTION OPTIONS TEMPLATE--> 

       <StackPanel Orientation="Horizontal"> 
        <Image Source="Assets/Resources/ImageResources/ManufacturingICON.png" Width="20" Height="20"/> 
        <TextBlock Margin="0,0,0,0" Text="{Binding Path=Name}"/> 
       </StackPanel> 
      </common:HierarchicalDataTemplate> 
     </common:HierarchicalDataTemplate.ItemTemplate> 
     <!--END OF AREA OPTIONS TEMPLATE--> 

     <StackPanel Orientation="Vertical">       
          <StackPanel Orientation="Horizontal"> 
        <Image Source="Assets/Resources/ImageResources/ManufacturingICON.png" Width="20" Height="20"/> 
        <TextBlock Margin="0,0,0,0" Text="{Binding Path=Name}"/>      
       </StackPanel> 
          <sdk:TreeView HorizontalAlignment="Left" ItemsSource="{Binding Source={StaticResource locationInspectionsViewSource}}" Name="inspectionsTreeView" VerticalAlignment="Top" ItemTemplate="{StaticResource Level2}" BorderBrush="{x:Null}" Background="{x:Null}"/>           
        </StackPanel> 
    </common:HierarchicalDataTemplate> 
    <!--END OF NAVIGATION TEMPLATE--> 

謝謝

回答

2

After我發現:

沒有,使用HierarchicalDataTemplate顯示嵌套的TreeView層次結構時是不可能的。數據模板只允許每個節點有一個「孩子」。

一個解決方案是將兩個列表項「子」合併到一個列表「子」並在層次結構中使用。 因此,在我的情況下,地點將與單個「孩子」表相關,其中地區和檢查實體相鄰,每個地區與相關的檢查兒童有關,並且檢查沒有孩子。

第二個解決方案是使用嵌套的DataGrid。這似乎是作弊,但它取得了預期的效果。模板將需要爲DataGrid進行更改,以便沒有列標題/備用行着色/高亮等等。取決於TreeView需要的樣子。

其實,它更容易,因爲它使用標準的DomainDataSource作爲根表(帶有所有內含),它允許輕鬆封裝DataContext,這也意味着仍然沒有代碼隱藏以確保佈局是分開的。

可能有更多的方法,但我找到了一個適合我的解決方案,很簡單。

+0

我現在正在實施第一種解決方案,因爲我的需求已經有所變化,現在第一種解決方案更適用於我的需求。它有助於使用自定義列表和佈局以及延遲加載,如果層次結構龐大,這將有所幫助。 – xhedgepigx