复杂的 Silverlight TreeView,是否可以嵌套层次结构? [英] Complex Silverlight TreeView, is nested hierarchy possible?

查看:39
本文介绍了复杂的 Silverlight TreeView,是否可以嵌套层次结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的数据库:

I have a DB that looks like:

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

所以每个位置可以有零个或多个检查和零个或多个区域,并且区域有零个或多个检查.检查的记录都具有 LocationID!=null 和 AreaID=null 或 !=null 以获得此层次结构.

So each Location can have zero or more Inspections and zero or more Areas, and Areas have zero or more Inspections. The records for Inspections all have a LocationID!=null and the AreaID=null or !=null to get this hierarchy.

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

I would like to get all the names of each item in the table in a treeview as navigation. So far I can get EITHER

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

Locations-->Areas-->Inspections OR

地点-->检查

我似乎无法让树视图层次结构显示我需要的内容.是否可以?我尝试使用嵌套树视图作为层次结构中的一个项目来显示我想要的内容,但它无法正常工作.

I cannot seem to get the treeview hierarchy to show what I need. Is it possible? I have tried to use a nested treeview as an item in the hierarchy to show what I want but it doesn't work correctly.

位置-->区域-->检查的xaml代码

xaml code for Locations-->Areas-->Inspections

 <!--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-->检查

xaml for Locations-->Inspections

 <!--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

xaml for nested treeview

 <!--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-->

谢谢

推荐答案

经过大量研究、拉扯头发和办公桌后我发现:

After much research, hair-pulling and head-desking I found that:

,在使用 HierarchicalDataTemplate 显示嵌套的 TreeView 层次结构时是不可能的.数据模板只允许每个节点有一个子节点".

No, it is not possible when using HierarchicalDataTemplate to display a nested TreeView hierarchy. The data template allows only for one 'child' per node.

一种解决方案是将两个列表项子项"合并为一个列表子项",并在层次结构中使用它.因此,在我的情况下,Locations 将与单个子"表相关,其中区域和检查实体相邻,每个区域与相关检查子项相关,检查没有子项.

One solution is to merge the two list items 'children' into one list 'child' and use that within the hierarchy. So in my case the Locations will relate to a single 'child' table where Areas and Inspections entities are adjacent, with each Area relating to the relevant Inspection children and the Inspections have no children.

第二种解决方案是使用嵌套的 DataGrid.这看似作弊,却达到了预期的效果.需要为 DataGrid 更改模板,以便没有列标题/备用行着色/高亮显示等.这取决于 TreeView 需要的外观.

A second solution is to use a nested DataGrid. This seems like cheating but it achieved the desired effect. The templates will need to be changed for the DataGrid so that there are have no column headers/alternate row colouring/higlighting etc. Depending on what the TreeView needs to look like.

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

The second is, in fact, the easier option as it uses the standard DomainDataSource for the root table (with all inclusions) which allows for easy encapsulation of DataContext, it also means there is still no code-behind to ensure the layout is separate.

可能有更多方法,但我找到了一个对我有用且简单的解决方案.

There may be more ways, but I have found a solution that works for me and is easy.

这篇关于复杂的 Silverlight TreeView,是否可以嵌套层次结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆