与ListBox的绑定将替换旧数据

本文关键字:替换 数据 绑定 ListBox | 更新日期: 2023-09-27 17:58:43

我使用的是像这样的布局xaml文件

<TextBlock x:Name="Teacher_header" Style="{StaticResource PhoneTextTitle2Style}" HorizontalAlignment="Center" Grid.Row="1"></TextBlock>
        <ListBox x:Name="listBox" Grid.Row="2" >
                       <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                    <DataTemplate>
                    <StackPanel x:Name="stackPanel_template"  Margin="0,12,0,12" Background="BurlyWood" >
                            <Grid x:Name="LayoutList2">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="{Binding Teacher}" 
                                           Style="{StaticResource PhoneTextNormalStyle}" 
                                           Grid.Row="0"  
                                           HorizontalAlignment="Left"
                                           FontWeight="Bold"
                                           />
                                <TextBlock Name="subjectName" 
                                           Text="{Binding Subject}" 
                                           Style="{StaticResource PhoneTextNormalStyle}" 
                                           Grid.Row="1" Grid.ColumnSpan="2" 
                                           HorizontalAlignment="Center"/>
                                <TextBlock Text="{Binding Session}" 
                                           Style="{StaticResource PhoneTextNormalStyle}" 
                                           Grid.Row="2" Grid.Column="0" 
                                           HorizontalAlignment="Center" />
                                <TextBlock Text="{Binding Group}" 
                                           Style="{StaticResource PhoneTextNormalStyle}" 
                                           Grid.Row="2" Grid.Column="1" 
                                           HorizontalAlignment="Center"/>
                                <!--<Button Grid.Row="3" Grid.Column="1" 
                                        HorizontalAlignment="Right" 
                                        Click="delete_record"
                                        BorderThickness="0">
                                    <Image Source="/Images/appbar.delete.rest.png"
                                           Width="75"
                                           Height="75"/>
                                </Button>-->
                                <toolkit:ContextMenuService.ContextMenu>
                                    <toolkit:ContextMenu Name="ContextMenu" >
                                        <toolkit:MenuItem 
                                Name="Delete"  
                                Header="Delete" 
                                Click="delete_record"/>
                                        <toolkit:MenuItem 
                                Name="Details"  
                                Header="Details" 
                                Click="teacher_detail_Hold"/>
                                        <toolkit:MenuItem 
                                Name="Edit"  
                                Header="Edit" 
                                Click="Edit_Details"/>
                                    </toolkit:ContextMenu>
                                </toolkit:ContextMenuService.ContextMenu>
                            </Grid>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
        </ListBox>

我正在读取xml文件,并将数据与ListBox绑定,如下所示。

foreach (XElement Teacher in loadedData.Descendants("Root").Descendants("Teachers").Descendants("Teacher"))
{
    string teacher = Teacher.Value.ToString();
    Teacher_header.Text = teacher;
    //GetnDisplayTeachersTable(teacher);
    var data = from record in loadedData.Descendants("Root").Descendants("Schedules").Descendants("schedule")
               where record.Attribute("teacher").Value == teacher
               select new TimeTable
               {
                   Teacher = (string)record.Attribute("teacher").Value,
                   Subject = (string)record.Attribute("name"),
                   Session = (string)record.Attribute("session"),
                   Group = (string)record.Attribute("group")
               };
    listBox.ItemsSource = data;
}

但从文件中读取的每个新教师数据都会替换以前的数据。而不是在末尾附加。

我希望它看起来像这样。

教师1主题组会话

教师2主题组会话

与ListBox的绑定将替换旧数据

您需要将数据添加到List<TimeTable>中,然后在读取所有数据后,将ItemsSource设置到此列表中。

List<TimeTable> MyList = new List<TimeTable>(); // create the list
foreach (XElement Teacher in loadedData.Descendants("Root").Descendants("Teachers").Descendants("Teacher"))
{
    string teacher = Teacher.Value.ToString();
    Teacher_header.Text = teacher;
    //GetnDisplayTeachersTable(teacher);
    var data = from record in loadedData.Descendants("Root").Descendants("Schedules").Descendants("schedule")
               where record.Attribute("teacher").Value == teacher
               select new TimeTable
               {
                   Teacher = (string)record.Attribute("teacher").Value,
                   Subject = (string)record.Attribute("name"),
                   Session = (string)record.Attribute("session"),
                   Group = (string)record.Attribute("group")
               };
    MyList.Add(data);
}
// finally after all the read set the source
listBox.ItemsSource = MyList;