【发布时间】:2014-04-13 13:11:28
【问题描述】:
我的 WPF 中有一个 Datagrid,通过双击另一个 DataGrid 上的 ObservableCollection 填充了一个 DataGrid(用户单击首选标题和详细信息 DataGrid 填充了相关数据)。
每次我搜索新标题并进行选择时,详细信息 DataGrid 都会替换已经存在的第一行数据。如何使 DataGrid 添加一个新行,以便我可以一次记录多个实体以进行导出?
我的代码如下:
数据网格.cs
// 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_MouseDoubleClick(object sender, RoutedEventArgs e)
{
// Grabs the IMDBID associated to the movie title selected to be used with the second API request.
// DataRowView row = (DataRowView)Movie_List.SelectedItems;
string titleID = ((searchInfo)Movie_List.SelectedItem).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.
var doc = XElement.Load(newurl);
// Creates a XML Noedlist to store the values that are going to be associated with the given attribute tag.
IEnumerable<XElement> movieList = doc.Descendants("movie");
// Creats an ObservableCollection for the Object Retrievalinfo to be displayed in the Movie_Datagrid, that can be edited.
ObservableCollection<Retrievalinfo> gridInfo = new ObservableCollection<Retrievalinfo>(movieList.Select(movieElement =>
new Retrievalinfo()
{
title = movieElement.Attribute("title").Value,
actors = movieElement.Attribute("actors").Value.Split(',').ToList(),
genre = movieElement.Attribute("genre").Value,
rated = movieElement.Attribute("rated").Value,
imdbRating = movieElement.Attribute("imdbRating").Value,
released = movieElement.Attribute("released").Value,
runtime = movieElement.Attribute("runtime").Value,
}));
Movie_DataGrid.ItemsSource = gridInfo;
}
Retrievalinfo 类(用于 ObservableCollection 的对象)
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; }
}
}
DataGird XAML(Movie_DataGrid,保存所选电影的详细信息)
<DataGrid x:Name="Movie_DataGrid"
Grid.ColumnSpan="2"
HorizontalAlignment="Left"
Margin="292,107,0,0"
VerticalAlignment="Top"
Height="214"
Width="673"
AutoGenerateColumns="False"
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=actors[2]}"/>
<DataGridComboBoxColumn Header="Digital Format" ItemsSource="{Binding digital}"/>
<DataGridComboBoxColumn Header="Physical Format" ItemsSource="{Binding physical}"/>
<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}"/>
<DataGridComboBoxColumn Header="File Type" ItemsSource="{Binding file_type}"/>
</DataGrid.Columns>
</DataGrid>
【问题讨论】: