在UWP中绑定多对多关系

时间:2017-01-09 08:07:25

标签: c# sqlite data-binding uwp

问题:我有一个包含3个表的数据库:教师;类;和Teacher_Class; 老师可以教很多课,班上可以有很多老师。我可以绑定并显示教师列表或班级列表,但我不知道如何在教师列表中添加另一个列表,显示他们正在教授哪个班级。为.Net c#找到了一些解决方案,但没有为UWP找到解决方案。这是我的代码:

MainPage.xaml中:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="50*"></RowDefinition>
        <RowDefinition Height="300*"></RowDefinition>
    </Grid.RowDefinitions>
    <TextBlock Text="Teacher list" FontSize="25" Grid.Row="0"></TextBlock>
    <ListView x:Name="listView" Grid.Row="1">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="data:TeacherWithClasses">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="{x:Bind teacher.TeacherID}"></TextBlock>
                    <TextBlock Text="{x:Bind teacher.TeacherName}"></TextBlock>
                    <TextBlock Text="Teaching class(es):"></TextBlock>
                    <ListView ItemsSource="{x:Bind listOfClasses}">
                        <ListView.ItemTemplate>
                            <DataTemplate x:DataType="data:Class">
                                <TextBlock Text="{x:Bind ClassName}"></TextBlock>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

代码背后:

public MainPage()
    {
        this.InitializeComponent();
        string path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "SchoolDatabase.sqlite");
        SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path);          
        conn.CreateTable<Teacher>();
        conn.CreateTable<Class>();

        List<TeacherWithClasses> temp = conn.Query<TeacherWithClasses>(@"select t.TeacherID, t.TeacherName, c.ClassID, c.ClassName from Class c join Teacher_Class tc on c.ClassID = tc.ClassID join Teacher t on tc.TeacherID = t.TeacherID");
        ObservableCollection<TeacherWithClasses> listOfTeachers = new ObservableCollection<TeacherWithClasses>(temp);
        listView.ItemsSource = listOfTeachers;
    }

类别:

class Teacher
{
    [PrimaryKey, AutoIncrement]
    public int TeacherID { get; set; }
    public string TeacherName { get; set; }

    public Teacher() { }
}

class Class
{
    [PrimaryKey, AutoIncrement]
    public int ClassID { get; set; }
    public string ClassName { get; set; }

    public Class() { }
}

class TeacherWithClasses
{
    public Teacher teacher;
    public List<Class> listOfClasses;

    public TeacherWithClasses(Teacher _teacher, List<Class> _listOfClasses)
    {
        teacher = _teacher;
        listOfClasses = _listOfClasses;
    }

    //public TeacherWithClasses() { }
} 

SQLite的:

insert into Teacher (TeacherName) values ('Lee Jun Hoo');
insert into Teacher (TeacherName) values ('Marthew Andy');

insert into Class (ClassName) values ('1A');
insert into Class (ClassName) values ('1B');
insert into Class (ClassName) values ('1C');

create table Teacher_Class
(
TeacherID int not null,
ClassID int not null,
foreign key(TeacherID) references Teacher(TeacherID),
foreign key(ClassID) references Class(ClassID)
);

insert into Teacher_Class values (1,1);
insert into Teacher_Class values (1,2);
insert into Teacher_Class values (2,2);
insert into Teacher_Class values (2,3);

该应用程序现在:

enter image description here

我需要什么:

enter image description here

更新新问题:我尝试使用Backstar建议并且卡在他离开的地方,我试图用这些代码填充列表:

        List<TeacherWithClasses> temp = conn.Query<TeacherWithClasses>(@"select t.TeacherID, t.TeacherName, c.ClassID, c.ClassName from Class c join Teacher_Class tc on c.ClassID = tc.ClassID join Teacher t on tc.TeacherID = t.TeacherID");
        ObservableCollection<TeacherWithClasses> listOfTeachers = new ObservableCollection<TeacherWithClasses>(temp);
        listView.ItemsSource = listOfTeachers;

第一个查询的异常,列表temp:类型&#39; App3.TeacherWithClasses&#39;上的构造函数未找到。所以我添加了一个空构造函数,没有异常,应用程序运行,但没有显示绑定文本。

如果您能在代码中显示答案或只是链接到示例,我很感激,这个问题的教程也很好。提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果您创建一个自定义类,它可以连接您希望在列表的子列表中显示的所有数据,则它可以工作。

  1. 创建一个新类,以便在列表中显示所有属性 及其子列表。在这个类中声明一个将保持的新列表 与教师有关的课程项目。
  2. 创建一个包含此新类的新列表。
  3. 填写清单。
  4. 将其作为ItemSource添加到列表中。绑定到子列表。使用路径 必要的财产。
  5. 示例:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="50*"></RowDefinition>
            <RowDefinition Height="300*"></RowDefinition>
        </Grid.RowDefinitions>
        <TextBlock Text="Teacher list" FontSize="25" Grid.Row="0"></TextBlock>
        <ListView x:Name="listView" Grid.Row="1" ItemSource="{x:Bind listOfTeachers"}>
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="TeacherWithClasses">
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{x:Bind Path=teacher.TeacherID}"></TextBlock>
                        <TextBlock Text="{x:Bind Path=teacher.TeacherName}"></TextBlock>
                        <TextBlock Text="Teaching class(es):"></TextBlock>
                        <ListView ItemSource="{x:Bind listOfClasses}">
                            <ListView.ItemTemplate>                     
                                <DataTemplate x:DataType="Class">
                                    <TextBlock Text="{x:Bind ClassName}" />
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>                    
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
    
    class TeacherWithClasses
    {
        public Teacher teacher;    
        List<Class> listOfClasses;
    
        public TeacherWithClasses(Teacher _teacher, List<Class> _listOfClasses)
        {
            teacher = _teacher;
            listOfClasses = _listOfClasses;
        }
    }
    
    class Class
    {
        public int ClassID { get; set; }
        public string ClassName { get; set; }
    
        public Class() { }
    }
    

    然后你必须创建listOfTeachers List:

    ObservableCollection<TeacherWithClasses> listOfTeachers = fill the list with items