簡體   English   中英

在Visual Studio或Expression Blend中將TreeView連接到.mdb數據庫

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

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

我一直在嘗試使TreeView連接到在Visual Studio(使用Silverlight Business Application)中已連接的.mdb數據庫,以在每個表中顯示名稱屬性的導航樹。 我有2個等級的層次結構:

(除了顯示的屬性以外,還有更多屬性,但這是唯一需要的)

  1. 根級別: 位置表 [LocationName屬性]
  2. 第一層: 面積表 [AreaName屬性] [LocationID]
  3. 第二層: 檢查表 [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>

域服務(c#)GetLocationsQuery

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

這可能與所使用的查詢有關嗎? 我是否應該將樹視圖所需的信息放入GetLocationsQuery中?

  • 如果是這樣,如何在查詢中返回位置名稱,子區域名稱和子檢查名稱的列表?

先感謝您。

找到了解決方案,如果需要,我將為其他人發布:

需要更改的是域服務和元數據信息=>

在元數據文件中,您希望服務回傳的表中的每個EntityCollection需要[包含],例如:

位置表

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

面積表

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

檢驗表

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

在域服務文件中,使用的查詢要求:

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

其中每個“ .Entity”是元數據文件中包含的集合實體的名稱。

回到xaml-只要綁定路徑名稱與EntityCollection名稱相同,它就可以與代碼一起使用。

:)希望一直有用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM