简体   繁体   English

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

[英]Connect TreeView to .mdb Database in Visual Studio or Expression Blend

I'm a newbie to Visual Studio (2010) and Expression Studio (4). 我是Visual Studio(2010)和Expression Studio(4)的新手。

I have been trying to get a TreeView to connect to a .mdb database that I have connected in Visual Studio (using Silverlight Business Application) to show a Navigation Tree for the Name Properties in each table. 我一直在尝试使TreeView连接到在Visual Studio(使用Silverlight Business Application)中已连接的.mdb数据库,以在每个表中显示名称属性的导航树。 I have 2 levels of hierarchy: 我有2个等级的层次结构:

(there are many more properties than shown but these are the only required) (除了显示的属性以外,还有更多属性,但这是唯一需要的)

  1. Root Level: Location Table [LocationName Property] 根级别: 位置表 [LocationName属性]
  2. First Level: Area Table [AreaName Property] [LocationID] 第一层: 面积表 [AreaName属性] [LocationID]
  3. Second Level: Inspection Table [InspectionName Property] [AreaID] 第二层: 检查表 [InspectionName属性] [AreaID]

I have tried many ways to connect and none seem to work - I am quite happy now with making a TreeView template with a connection to hierarchical Sample Data created in Expression Blend. 我尝试了多种连接方式,但似乎都没有效果-现在,我很高兴制作一个TreeView模板,并连接到Expression Blend中创建的分层示例数据。 Unfortunately, I can only seem to make a connection with the top level of my real database - so it only shows the names of the locations and won't expand any further. 不幸的是,我似乎只能与真实数据库的顶层建立连接-因此它仅显示位置名称,并且不会进一步扩展。

I have no idea what to do. 我不知道该怎么做。 The code I'm using is: (no code-behind) 我正在使用的代码是:(无代码隐藏)

Home.xaml 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>

Navigation Tree Resource Dictionary 导航树资源字典

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

Domain Service (c#) GetLocationsQuery 域服务(c#)GetLocationsQuery

 public IQueryable<Location> GetLocations()
    {
        return this.ObjectContext.Locations.OrderBy(l=>l.Name);
    }

Is it perhaps something to do with the Query used? 这可能与所使用的查询有关吗? Should I be putting the information I need for the treeview in the GetLocationsQuery? 我是否应该将树视图所需的信息放入GetLocationsQuery中?

  • If so how do I put in the query to return a list of location names, child area names and child inspection names? 如果是这样,如何在查询中返回位置名称,子区域名称和子检查名称的列表?

Thank you in advance. 先感谢您。

Have found a solution, I will post for others in the case it is needed: 找到了解决方案,如果需要,我将为其他人发布:

It was the domain service and metadata info that needed to be changed => 需要更改的是域服务和元数据信息=>

In the metadata file, each EntityCollection in a table that you want the service to pass back needs [Include] eg: 在元数据文件中,您希望服务回传的表中的每个EntityCollection需要[包含],例如:

Locations Table 位置表

[Include]
public EntityCollection<Area> Areas { get; set; }

Areas Table 面积表

[Include]
public EntityCollection<Inspection> Inspections { get; set; }

Inspections Table 检验表

[Include]
public EntityCollection<InspectionItem> InspectionItems { get; set; }

and in the domain service file the query used requires: 在域服务文件中,使用的查询要求:

public IQueryable<Location> GetLocationsAndSubCategories()
    {
        return this.ObjectContext.Locations.Include("Areas.Inspections");
    }

where each ".Entity" is the name of the collection entities that have been included in the metadata file. 其中每个“ .Entity”是元数据文件中包含的集合实体的名称。

Back to the xaml - it should work with the code as long as the Binding path names are the same as the EntityCollection Names. 回到xaml-只要绑定路径名称与EntityCollection名称相同,它就可以与代码一起使用。

:) Hope that has been useful :)希望一直有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM