删除绑定到xml的特定列表框项
本文关键字:列表 绑定 xml 删除 | 更新日期: 2023-09-27 17:58:09
我有一个绑定到xml文件的列表框。
<ListBox x:Name="listBox" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Hold="teacher_detail_Hold" Margin="0,12,0,12">
<TextBlock Text="{Binding Teacher}" Style="{StaticResource PhoneTextNormalStyle}" Grid.Row="0" Grid.ColumnSpan="2" HorizontalAlignment="Center"/>
<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="Left" />
<TextBlock Text="{Binding Group}" Style="{StaticResource PhoneTextNormalStyle}" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Right"/>
<Button Grid.Row="3" Content="delete" Click="delete_record"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这是我的数据类
public class TimeTable
{
string teacher;
string subject;
string session;
string group;
string time;
//Teacher Property
public string Teacher
{
get { return teacher; }
set { teacher = value; }
}
//Subject Property
public string Subject
{
get { return subject; }
set { subject = value; }
}
//Session Propert
public string Session
{
get { return session; }
set { session = value; }
}
//Group Property
public string Group
{
get { return group; }
set { group = value; }
}
//Time Property
public string Time
{
get { return time; }
set { time = value; }
}
}
这就是我正在使用的xml文件。
<Day id="Monday">
<subject sbjname="Software Testing" session="2010" group="Alpha" teacher="Tasawar Khan" classroom="Class Room 1" time="8:30">
</subject>
</Day>
<Day id="Tuesday">
<subject sbjname="Digital Image Processing" session="2011" group="Alpha" teacher="Ali Javed" classroom="Class Room 2" time="11:30">
</subject>
</Day>
我想在按下删除按钮时删除一个条目。我想删除基于sbjname属性的数据。这是删除按钮点击方法
private void delete_record(object sender, RoutedEventArgs e)
{
}
我知道如何从xml文件中删除。但我不知道如何从列表框中选择的项目中获得主题名称
YourDataClass item = listBox.SelectedItem as YourDataClass ;
YourdataList.Remove(item);