简体   繁体   中英

How can I display related data from SQLite in XAML for UWP apps?

Visual Studio debugging shows that I have successfully retrieved the data from the database. My database has two tables, with the Terms table containing a foreign key from the Years table, as seen below:

public class Term
{

    [Key]
    public int TermId { get; set; }
    public string Name { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }


    public int YearId { get; set; }
    public Year Year { get; set; }
}

public class Year
{
    [Key]
    public int YearId { get; set; }
    public string YearName { get; set; }
    public string School { get; set; }
    public string Grade { get; set; }

    public List<Term> Terms { get; set; }
}

XAML:

<GridView ItemsSource="{Binding Source={StaticResource YearDisplay}}">
   <GridView.ItemTemplate>
      <DataTemplate>
         <StackPanel Margin="5">
            <TextBlock Text="{Binding YearId}" />
            <TextBlock Text="{Binding YearName}" />
         </StackPanel>
      </DataTemplate>
   </GridView.ItemTemplate>
</GridView>

How can I display a Year so that it contains all the Terms which reference it?

I've spent ages trying to figure this out, and I would really appreciate it if any help was given ☺🙏

Use another GridView control (or ListView) for list of Terms:

<GridView ItemsSource="{Binding Source={StaticResource YearDisplay}}">
   <GridView.ItemTemplate>
      <DataTemplate>
         <StackPanel Margin="5">
            <TextBlock Text="{Binding YearId}" />
            <TextBlock Text="{Binding YearName}" />
            <GridView ItemsSource="{Binding Terms}">
               <GridView.ItemTemplate>
                  <DataTemplate>
                     <TextBlock Text="{Binding Name}" />
                     <!-- ..... -->
                  </DataTemplate> 
               </GridView.ItemTemplate>
            </GridView>
         </StackPanel>
      </DataTemplate>
   </GridView.ItemTemplate>
</GridView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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