与ListBox的绑定替换旧数据

时间:2014-04-16 18:17:59

标签: c# xml windows-phone-8 listbox

我正在使用像这样的布局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;
}

但是从文件中读取的每个新教师数据都会替换前一个。而不是追加到最后。

我希望它看起来像这样。

Teacher1 学科 组 会话

Teacher2 学科 组 会话

1 个答案:

答案 0 :(得分:0)

您需要将数据添加到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;