我有一个像这样的xml文件
<Root>
<Day id="Monday">
<subject name="Software Testing" session="2010" group="Alpha" teacher="Tasawar Khan" classroom="Class Room 1" time="8:30">
</subject>
</Day>
<Day id="Tuesday">
<subject name="Digital Image Processing" session="2011" group="Alpha" teacher="Ali Javed" classroom="Class Room 2" time="11:30">
</subject>
</Day>
<Day id="Wednesday">
<subject name="Digital Image Processing" session="2011" group="Alpha" teacher="Ali Javed" classroom="Class Room 2" time="11:30">
</subject>
</Day>
<Day id="Thursday">
<subject name="Digital Image Processing" session="2011" group="Alpha" teacher="Ali Javed" classroom="Class Room 2" time="11:30">
</subject>
</Day>
<Day id="Friday">
<subject name="Digital Image Processing" session="2011" group="Alpha" teacher="Ali Javed" classroom="Class Room 2" time="11:30">
</subject>
</Day>
</Root>
我正在阅读并将此文件绑定到ListBox,如下所示
string TeachersXMLPath = "Teachers.xml";
XDocument loadedData = XDocument.Load(TeachersXMLPath);
var data = (from query in loadedData.Descendants("Day").Descendants("subject")
select new TimeTable
{
Teacher = (string)query.Attribute("teacher").Value,
Subject = (string)query.Attribute("name"),
Session = (string)query.Attribute("session"),
Group = (string)query.Attribute("group")
});
listBox.ItemsSource = data;
这是我的TimeTable类:
public class TimeTable
{
string teacher;
string subject;
string session;
string group;
string time;
string classroom;
//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; }
}
public string Location
{
get { return classroom; }
set { classroom = value; }
}
}
这是我的ListBox
<ListBox x:Name="listBox" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Hold="teacher_detail_Hold" Margin="0,12,0,12">
<Grid x:Name="LayoutList2" Background="BurlyWood">
<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" 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" Grid.Column="1"
HorizontalAlignment="Right"
Click="delete_record"
BorderThickness="0">
<Image Source="/Images/appbar.delete.rest.png"
Width="75"
Height="75"/>
</Button>
</Grid>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
显示数据时效果很好。
我想要的是将ObservableCollection绑定到ListBox ,这样当点击删除按钮时,我也可以从列表中删除所选项目。
答案 0 :(得分:0)
创建一个ViewModel
课程。
这个ViewModel
类应该实现INotifyPropertyChanged
。
声明ObservableCollection
类TimeTable
。
将ListBox的DataContext
设置为ViewModel
。
将ListBox的ItemsSource
设置为ObservableCollection
。
您的ViewModel将如下所示:
public class SomeViewModel: INotifyPropertyChanged{
private ObservableCollection<TimeTable> _timeTableCollection;
public ObservableCollection<TimeTable> TimeTableCollection{
get{
return _timeTableCollection;
}
set{
_timeTableCollection = value;
onPropertyChanged(this, "TimeTableCollection");
}
}
// Declare the PropertyChanged event
public event PropertyChangedEventHandler PropertyChanged;
// OnPropertyChanged will raise the PropertyChanged event passing the
// source property that is being updated.
private void onPropertyChanged(object sender, string propertyName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
}
}
}