ListView绑定到List

时间:2017-02-12 19:01:09

标签: c# windows xaml data-binding uwp

我正在构建一个可以显示成绩的学校应用程序。 我有以下DataStructure:

public class Rootobject
{
    public List<Subject> subjects{ get; set; }
}

public class Subject
{
    public String name { get; set; }
    public int id { get; set; }
    public String teacher { get; set; }
    public GradeSet wirtten { get; set; }
    public GradeSet spoken { get; set; }
    public float average { get; set; }
}

public class GradeSet
{
    public float counts { get; set; }
    public List<Grade> grades { get; set; }
    public float average { get; set; }
}

public class Grade
{
    public string name { get; set; }
    public string grade { get; set; }
}

我有一个来自“主题”类型的ObservableCollection

subjects = new ObservableCollection<Subject>();

我有3个ListViews。一个显示所有主题(姓名和老师)。这已经有效了。 我是如何约束的:

<ListView Name="SubjectsListView" IsItemClickEnabled="True" ItemsSource="{x:Bind subjects}" ItemClick="FacherListView_ItemClick">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="data:Subject">
            <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="{x:Bind name}" FontSize="20" Margin="4,0,0,0" />
                <TextBlock Text="{x:Bind teacher}" Grid.Row="1" Margin="4,4,0,0" />
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

在其他2个ListView中,在第一个中,我想显示书面成绩(姓名和成绩本身),在第二个中,我想显示口语成绩(姓名和成绩本身)。 书面和口语成绩ListView看起来相同,但如何将成绩和名称绑定到它们? 这是ListView:

<ListView Name="gradeView" Grid.Column="0" HorizontalContentAlignment="Stretch" Grid.Row="2" SelectionMode="None">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid Margin="4">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <StackPanel Orientation="Vertical">
                    <TextBlock Name="GradeName" Text="The name of the grade"  FontSize="20" FontWeight="Bold" />
                    <TextBlock Name="GradeName" Text="the grade (B+)" FontSize="20" />
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

2 个答案:

答案 0 :(得分:1)

由于每个科目都有书面和口语成绩,因此您可以绑定&#34; gradeView&#34;的ItemsSource属性。到&#34; SubjectsListView&#34;:

SelectedItem属性
<ListView Name="gradeView" Grid.Column="0" HorizontalContentAlignment="Stretch" Grid.Row="2" SelectionMode="None"
                  ItemsSource="{Binding SelectedItem.wirtten.grades, ElementName=SubjectsListView}">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="data:Grade">
            <Grid Margin="4">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <StackPanel Orientation="Vertical">
                    <TextBlock Name="GradeName" Text="{x:Bind name}"  FontSize="20" FontWeight="Bold" />
                    <TextBlock Name="GradeName" Text="{x:Bind grade}" FontSize="20" />
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

第三个ListView几乎相同。只需更改ItemsSource绑定的路径:

<ListView Name="gradeView2" Grid.Column="0" HorizontalContentAlignment="Stretch" Grid.Row="2" SelectionMode="None"
                  ItemsSource="{Binding SelectedItem.spoken.grades, ElementName=SubjectsListView}">

当您在第一个ListView中选择相应的主题时,应填充第二个和第三个ListView

答案 1 :(得分:0)

这是未经测试的,但您可以尝试添加嵌套的ListView来显示成绩,如下所示:

<ListView Name="SubjectsListView"
          IsItemClickEnabled="True"
          ItemsSource="{x:Bind subjects}"
          ItemClick="FacherListView_ItemClick">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="data:Subject">
            <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="{x:Bind name}"
                           FontSize="20"
                           Margin="4,0,0,0" />


                <ListView IsItemClickEnabled="True"
                          ItemsSource="{x:Bind wirtten.grades}"
                          Grid.Row="1"
                          Margin="4,4,0,0">
                    <ListView.ItemTemplate>
                        <DataTemplate x:DataType="data:Grade">
                            <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="{x:Bind name}"
                                           FontSize="20"
                                           Margin="4,0,0,0" />
                                <TextBlock Text="{x:Bind grade}"
                                           Grid.Row="1"
                                           Margin="4,4,0,0" />
                            </Grid>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>



            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>