我有一个带有一些数据的xml文件,我想将其绑定到文本块。下面的代码不向文本块添加任何内容。我做错了。有什么建议吗?
XAML:
<GridView x:Name="DataGrid1">
<GridView.ItemTemplate>
<DataTemplate>
<Grid Background="AliceBlue" Width="300" Height="200">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Title}"></TextBlock>
<TextBlock Text="{Binding Category}"></TextBlock>
</StackPanel>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
C#
string XMLPath = Path.Combine(Package.Current.InstalledLocation.Path, "booksData/data.xml");
XDocument loadedD = XDocument.Load(XMLPath);
var newData = from query in loadedD.Descendants("element")
select new Book
{
Title = (string)query.Attribute("title"),
Category = (string)query.Attribute("category")
};
DataGrid1.ItemsSource = newData;
XML:
<books>
<element>
<category>Thriller</category>
<description>In The Green Line, </description>
<id>1</id>
<image>images/greenLine.jpg</image>
<price>10.50</price>
<title>The Green Line</title>
</element>
答案 0 :(得分:3)