我已阅读多个答案,但根本无法得到任何解决我的问题。我有一个从Web请求中接收XML的程序,在该请求中我只抓取该XML中的特定属性。在逐步浏览时,我可以看到一切正常,检索获取正确的XML,排序并获取正确的标记作品,然后将每个标记的内容分配给其在另一个类中的相关变量。另外,当逐步完成时,“gridInfo”显示一个列表,将显示我想要显示的每个值。绑定ObservableCollection列表
我无法工作的是将列表绑定到DataGrid。正如你所看到的,我创建了一个新的ObserableCollection类,其中存储了每个变量并调用该对象。我创建了该类的列表,并将XML中的值存储到该列表中。任何人都可以帮忙吗?
也在寻找我读了你会发现它不是在这里的一切都包含在它自己开始和结束标记内的正常格式的XML时,这就是为什么我不得不使用DocumentElement还有的getElementsByTagName
代码背后:
// This action will seach the IMDb API for the associated infromation for the IMDBID that is tagged with the title you chose in the ListBox.
private void Movie_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
{ // Grabs the IMDBID associated to the movie title selected to be used with the second API request.
var p = Movie_List.SelectedIndex;
string titleID = structholder[p].IMDBID;
string newurl = "http://www.omdbapi.com/?i=" + titleID + "&r=XML";
// Prepares 2nd API URL request to get data for chosen title.
// Creates a XML Document to store the xml data that was sent back by the API.
XmlDocument doc = new XmlDocument();
doc.Load(newurl);
// Creates a XML Noedlist to store the values that are going to be associated with the given attribute tag.
XmlNodeList movieList = doc.DocumentElement.GetElementsByTagName("movie");// GetElementsByTagName("root");
ObservableCollection<Retrievalinfo> test = new ObservableCollection<Retrievalinfo>();
List<Retrievalinfo> gridInfo = new List<Retrievalinfo>();
foreach (XmlNode node in movieList)
{
gridInfo.Add(new Retrievalinfo(){
title = node.Attributes["title"].Value.ToString(),
actors = node.Attributes["actors"].Value.Split(',').ToList(),
genre = node.Attributes["genre"].Value.ToString(),
rated = node.Attributes["rated"].Value.ToString(),
imdbRating = node.Attributes["imdbRating"].Value.ToString(),
released = node.Attributes["released"].Value.ToString(),
runtime = node.Attributes["runtime"].Value.ToString(),
});
}
Movie_DataGrid.ItemsSource = Retrievalinfo;
}
Retrivealinfo类别:
namespace WpfApplication3
{
public class Retrievalinfo
{
public Retrievalinfo()
{
actors = new List<string>();
}
//Creating a list of info objects that will store all returned data for selected title.
public string title { get; set; }
public List<string> actors { get; set; }
public string genre { get; set; }
public string rated { get; set; }
public string imdbRating { get; set; }
public string released { get; set; }
public string runtime { get; set; }
}
}
XAML:
<ListBox x:Name="Movie_List" ItemsSource="{Binding listInfo}" Grid.ColumnSpan="2" HorizontalAlignment="Left" Height="287" Margin="20,107,0,0" VerticalAlignment="Top" Width="198" SelectionChanged="Movie_List_SelectionChanged" />
<Label Grid.ColumnSpan="2" Content="Movie List" HorizontalAlignment="Left" Height="30" Margin="70,72,0,0" VerticalAlignment="Top" Width="99" FontSize="16" FontFamily="Cooper Black" />
<DataGrid x:Name="Movie_DataGrid" Grid.ColumnSpan="2" HorizontalAlignment="Left" Margin="232,107,0,0" VerticalAlignment="Top" Height="198" Width="497" AutoGenerateColumns="True" ItemsSource="{Binding gridInfo}">
<DataGrid.Columns>
<DataGridTextColumn Header="Title" Binding="{Binding Path=title}"/>
<DataGridTextColumn Header="Main Actor 1" Binding="{Binding Path=actors[0]}"/>
<DataGridTextColumn Header="Main Actor 2" Binding="{Binding Path=actors[1]}"/>
<DataGridTextColumn Header="Main Actor 3" Binding="{Binding Path=actor[2]}"/>
<DataGridTextColumn Header="Genre" Binding="{Binding Path=genre}"/>
<DataGridTextColumn Header="Rated" Binding="{Binding Path=rated}"/>
<DataGridTextColumn Header="IMDB Rating" Binding="{Binding Path=imdbRating}"/>
<DataGridTextColumn Header="Released" Binding="{Binding Path=released}"/>
<DataGridTextColumn Header="Runtime" Binding="{Binding Path=runtime}"/>
</DataGrid.Columns>
</DataGrid>
XML我读:
<root response="True">
<movie title="Up in the Air" year="2009" rated="R" released="23 Dec 2009" runtime="109
min" genre="Drama, Romance" director="Jason Reitman" writer="Walter Kirn (novel), Jason
Reitman (screenplay), Sheldon Turner (screenplay)" actors="George Clooney, Vera Farmiga,
Anna Kendrick, Jason Bateman" plot="With a job that has him traveling around the country
firing people, Ryan Bingham leads an empty life out of a suitcase, until his company
does the unexpected: ground him." language="English" country="USA" awards="Nominated for
6 Oscars. Another 64 wins & 66 nominations."poster="http://ia.mediaimdb.com/images/M/MV5BMTI3MzYxMTA4NF5BMl5BanBnXkFtZTcwMD
[email protected]@._V1_SX300.jpg" metascore="83" imdbRating="7.5" imdbVotes="215,961" imdbID="tt1193138" type="movie"/>
</root>
Movie_DataGrid.ItemsSource = Retrievalinfo; Retrievalinfo是一个类而不是一个集合。我感到惊讶的是编译。尝试Movie_DataGrid.ItemsSource = gridInfo; – Paparazzi