[英]Binding Data to Data Grid
我目前有一個URL請求帶回XML數據。 我將該數據存儲在讀取的文檔中,並查找某些屬性之間的信息,並將這些值分配給我指定的變量。 我的wpf DataGrid叫做Movie_DataGrid。 如何將這些數據提供給DataGrid會有任何幫助。
- 編輯 -
我用一種新的方式更新了我的代碼,我試圖得到我的結果。 當單步執行代碼的每一步時,XML存儲正常,並且Retrivalinfo類和Retrievalinfo convertedMovie = new Retrievalinfo()之間的所有標記屬性都相同,但是此方法的應用程序錯誤。
我的新問題是,屬性中的值沒有被抓取和存儲。 我還提供了一些我會得到的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
E4ODg3Mg@@._V1_SX300.jpg" metascore="83" imdbRating="7.5" imdbVotes="215,961" imdbID="tt1193138" type="movie"/>
</root>
// 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;
// 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("http://www.omdbapi.com/?i=" + titleID + "&r=XML");
// Creates a XML Noedlist to store the values that are going to be associated with the given attribute tag.
XmlNodeList movieList = doc.GetElementsByTagName("movie");
var movie = movieList.Item(0);
Retrievalinfo convertedMovie = new Retrievalinfo()
{
title = movie.Attributes["title"].ToString(),
actors = movie.Attributes["actors"].ToString().Split(',').ToList(),
genre = movie.Attributes["genre"].ToString(),
rated = movie.Attributes["rated"].ToString(),
imdbRating = movie.Attributes["imbdRating"].ToString(),
released = movie.Attributes["released"].ToString(),
runtime = movie.Attributes["runtime"].ToString(),
};
List<Retrievalinfo> gridInfo = new List<Retrievalinfo>();
Movie_DataGrid.ItemsSource = gridInfo;
下面是我想要在DataGrid中顯示的每個變量的類。
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;
public List<string> actors;
public string genre;
public string rated;
public string imdbRating;
public string released;
public string runtime;
}
}
我寫了一篇冗長的aswer,但是,這里有一個快速的樣本,你可以用作參考並自己弄清楚細節。 MVVM不包括在內:D
希望能幫助到你。
代碼隱藏
namespace MyMovies
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
Movies = new ObservableCollection<Movie>()
{
new Movie("Lock, Stock and Two Smoking Barrels", 4),
new Movie("Life of Brian", 5),
};
var addMovieCommand = new RoutedUICommand();
CommandManager.RegisterClassCommandBinding(typeof(Window),
new CommandBinding(
addMovieCommand,
(sender, args) => AddMovie(),
(sender, args) => args.CanExecute = true));
AddMovieCommand = addMovieCommand;
}
public ObservableCollection<Movie> Movies { get; set; }
public ICommand AddMovieCommand { get; set; }
private void AddMovie()
{
Movies.Add(new Movie(Guid.NewGuid().ToString(), 3));
}
}
public class Movie
{
public Movie(string name, int stars)
{
Name = name;
Stars = stars;
}
public string Name { get; set; }
public int Stars { get; set; }
}
}
XAML
<Window x:Class="MyMovies.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<DataGrid
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ItemsSource="{Binding Movies}">
</DataGrid>
<Button Content="Add movie" Command="{Binding AddMovieCommand}" />
</StackPanel>
</Grid>
</Window>
哪個給你
如果我正確理解你的問題,你需要幾件:
ObservableCollection<RetrievalInfo>
用於存儲檢索到的數據 我很樂意為您不確定如何實施的任何或所有部件提供樣品。
您可以使用以下博客中提供的代碼段讀取xml到對象列表
博客鏈接:
http://danielwylie.me/blog/2010/04/c-convert-xml-to-an-object-or-list-of-an-object
您可以使用以下代碼段分配dataGrid的ItemSource
Movie_DataGrid.ItemsSource = list;
//here list object from public static List<T> XmlToObjectList<T>(string xml, string nodePath) method
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.