C# 将TreeView连接到Visual Studio或Expression Blend中的.mdb数据库

C# 将TreeView连接到Visual Studio或Expression Blend中的.mdb数据库,c#,visual-studio-2010,treeview,relational-database,expression-blend,C#,Visual Studio 2010,Treeview,Relational Database,Expression Blend,我是Visual Studio(2010)和Expression Studio(4)的新手 我一直在尝试使用TreeView连接到我在Visual Studio(使用Silverlight Business Application)中连接的.mdb数据库,以显示每个表中名称属性的导航树。我有两个层次结构: (有比所示更多的属性,但这些是唯一需要的) 根级别:位置表[LocationName属性] 第一级:面积表[AreaName属性][LocationID] 第二级:检查表[Inspection

我是Visual Studio(2010)和Expression Studio(4)的新手

我一直在尝试使用TreeView连接到我在Visual Studio(使用Silverlight Business Application)中连接的.mdb数据库,以显示每个表中名称属性的导航树。我有两个层次结构:

(有比所示更多的属性,但这些是唯一需要的)

  • 根级别:位置表[LocationName属性]
  • 第一级:面积表[AreaName属性][LocationID]
  • 第二级:检查表[InspectionName属性][AreaID]
  • 我已经尝试了很多方法来连接,但似乎都不起作用——我现在非常高兴能够创建一个TreeView模板,它可以连接到Expression Blend中创建的分层样本数据。不幸的是,我似乎只能与真实数据库的顶层建立连接,因此它只显示位置的名称,不会进一步扩展

    我不知道该怎么办。我使用的代码是:(没有代码隐藏)

    Home.xaml

     <riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my1:Location, CreateList=true}" Height="0" LoadedData="locationDomainDataSource_LoadedData_1" Name="locationDomainDataSource" QueryName="GetLocationsQuery" Width="0">
         <riaControls:DomainDataSource.DomainContext>
              <my:InspectDomainContext />
         </riaControls:DomainDataSource.DomainContext>
     </riaControls:DomainDataSource>
    
     <sdk:TreeView Height="200" ItemsSource="{Binding ElementName=locationDomainDataSource, Path=Data}" Name="locationTreeView1" Width="200" >
          <sdk:TreeView.Resources>
               <ResourceDictionary>
                    <ResourceDictionary.MergedDictionaries>
                         <ResourceDictionary Source="NavigationTreeResourceDictionary.xaml"/>
                    </ResourceDictionary.MergedDictionaries>
                </ResourceDictionary>
           </sdk:TreeView.Resources>
           <sdk:TreeView.ItemTemplate>
                <StaticResource ResourceKey="RootLevel"/>
           </sdk:TreeView.ItemTemplate>
       </sdk:TreeView>
    
    
    
    导航树资源字典

     <common:HierarchicalDataTemplate x:Key="Level2">
         <StackPanel Orientation="Horizontal">
             <TextBlock Margin="5,0,3,0" 
                   FontStyle="Italic" 
                   Text="{Binding Path=Name}" />                    
    
         </StackPanel>
     </common:HierarchicalDataTemplate>
    
     <common:HierarchicalDataTemplate x:Key="Level1"                 
        ItemsSource="{Binding Path=Inspections}"
        ItemTemplate="{StaticResource Level2}">
         <StackPanel Orientation="Horizontal">
             <TextBlock Margin="5,0,3,0" 
                   Text="{Binding Path=Name}" />
    
         </StackPanel>
     </common:HierarchicalDataTemplate> 
    
     <common:HierarchicalDataTemplate x:Key="RootLevel"
         ItemsSource="{Binding Path=Areas}"
         ItemTemplate="{StaticResource Level1}">
         <StackPanel Orientation="Horizontal">
             <TextBlock Margin="5,0,3,0"
                   Text="{Binding Path=Name}" 
                   FontWeight="Bold" FontSize="12" />
         </StackPanel>
     </common:HierarchicalDataTemplate>
    
     public IQueryable<Location> GetLocations()
        {
            return this.ObjectContext.Locations.OrderBy(l=>l.Name);
        }
    
    
    
    域服务(c#)GetLocationsQuery

     <common:HierarchicalDataTemplate x:Key="Level2">
         <StackPanel Orientation="Horizontal">
             <TextBlock Margin="5,0,3,0" 
                   FontStyle="Italic" 
                   Text="{Binding Path=Name}" />                    
    
         </StackPanel>
     </common:HierarchicalDataTemplate>
    
     <common:HierarchicalDataTemplate x:Key="Level1"                 
        ItemsSource="{Binding Path=Inspections}"
        ItemTemplate="{StaticResource Level2}">
         <StackPanel Orientation="Horizontal">
             <TextBlock Margin="5,0,3,0" 
                   Text="{Binding Path=Name}" />
    
         </StackPanel>
     </common:HierarchicalDataTemplate> 
    
     <common:HierarchicalDataTemplate x:Key="RootLevel"
         ItemsSource="{Binding Path=Areas}"
         ItemTemplate="{StaticResource Level1}">
         <StackPanel Orientation="Horizontal">
             <TextBlock Margin="5,0,3,0"
                   Text="{Binding Path=Name}" 
                   FontWeight="Bold" FontSize="12" />
         </StackPanel>
     </common:HierarchicalDataTemplate>
    
     public IQueryable<Location> GetLocations()
        {
            return this.ObjectContext.Locations.OrderBy(l=>l.Name);
        }
    
    public IQueryable GetLocations()
    {
    返回this.ObjectContext.Locations.OrderBy(l=>l.Name);
    }
    
    这可能与所使用的查询有关吗?我是否应该将树视图所需的信息放在GetLocationsQuery中?

    • 如果是,我如何输入查询以返回位置名称、子区域名称和子检查名称的列表

    提前谢谢。

    已经找到了解决方案,如果需要,我将为其他人发布:

    需要更改的是域服务和元数据信息=>

    在元数据文件中,希望服务传回的表中的每个EntityCollection都需要[包括],例如:

    位置表

    [Include]
    public EntityCollection<Area> Areas { get; set; }
    
    [Include]
    public EntityCollection<InspectionItem> InspectionItems { get; set; }
    
    [包括]
    公共实体集合区域{get;set;}
    
    面积表

    [Include]
    public EntityCollection<Inspection> Inspections { get; set; }
    
    [包括]
    公共EntityCollection检查{get;set;}
    
    检查表

    [Include]
    public EntityCollection<Area> Areas { get; set; }
    
    [Include]
    public EntityCollection<InspectionItem> InspectionItems { get; set; }
    
    [包括]
    公共EntityCollection检查项{get;set;}
    
    在域服务文件中,使用的查询需要:

    public IQueryable<Location> GetLocationsAndSubCategories()
        {
            return this.ObjectContext.Locations.Include("Areas.Inspections");
        }
    
    public IQueryable GetLocationsAndSubCategories()
    {
    返回此.ObjectContext.Locations.Include(“Areas.Inspections”);
    }
    
    其中,each“.Entity”是元数据文件中包含的集合实体的名称

    回到xaml——只要绑定路径名称与EntityCollection名称相同,它就应该与代码一起工作


    :)希望这很有用

    您也可以嵌套HierarchycalDataTemplates,而不是让它们相互引用。