C# 复杂的Silverlight树状视图,是否可以嵌套层次结构?

C# 复杂的Silverlight树状视图,是否可以嵌套层次结构?,c#,visual-studio-2010,xaml,treeview,silverlight-5.0,C#,Visual Studio 2010,Xaml,Treeview,Silverlight 5.0,我有一个DB,看起来像: Locations [rootlevel] Inspections [level1] Areas [level1] Inspections [level2] 因此,每个位置可以有零个或多个检查和零个或多个区域,并且区域可以有零个或多个检查。检查记录都有一个LocationID=null,且区域ID=null或=null以获取此层次结构 我想在树视图中获得表中每个项目的所有名称作为导航。到目前为止,我可以得

我有一个DB,看起来像:

Locations         [rootlevel]
   Inspections    [level1]
   Areas          [level1]
      Inspections [level2]
因此,每个位置可以有零个或多个检查和零个或多个区域,并且区域可以有零个或多个检查。检查记录都有一个LocationID=null,且区域ID=null或=null以获取此层次结构

我想在树视图中获得表中每个项目的所有名称作为导航。到目前为止,我可以得到任何一个

位置-->区域-->检查或

位置-->检查

 <!--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-->
 <!--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-->
我似乎无法让treeview层次结构显示我需要什么。可能吗?我曾尝试将嵌套的treeview用作层次结构中的一项,以显示我想要的内容,但它无法正常工作

位置-->区域-->检查的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-->
 <!--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="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-->
 <!--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-->


谢谢你

经过大量的研究、拔头发和整理头巾,我发现:

,使用HierarchycalDataTemplate显示嵌套的TreeView层次结构时不可能。数据模板只允许每个节点有一个“子节点”

一种解决方案是将两个列表项“子项”合并为一个列表“子项”,并在层次结构中使用。 因此,在我的案例中,位置将与单个“子”表相关,其中区域和检查实体相邻,每个区域与相关检查子对象相关,且检查没有子对象

第二种解决方案是使用嵌套数据网格。这看起来像作弊,但却达到了预期的效果。需要为DataGrid更改模板,以便不存在列标题/备用行着色/高光照明等,具体取决于TreeView的外观

第二个是,事实上,更简单的选项,因为它使用根表的标准DomainDataSource(包含所有内容),这允许轻松封装DataContext,也意味着仍然没有代码来确保布局是独立的


可能有更多的方法,但我已经找到了一个适合我且简单的解决方案。

我现在正在实施第一个解决方案,因为我的需求有所改变,现在第一个解决方案更适合我的需求。它有助于自定义列表和布局以及延迟加载,这在层次结构庞大时非常有用。