我的WPF中有一个Datagrid,在doubleclick表单上用一个ObservableCollection填充另一个DataGrid(用户点击首选标题和详细信息DataGrid用相关数据填充)。
每次搜索新标题,然后进行选择时,详细信息DataGrid将替换已存在的第一行数据。如何使DataGrid添加一个新行,以便我可以一次记录多个实体以进行导出?
My Code Bellow:
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>
答案 0 :(得分:0)
在绑定Datagrid之前,您应该将现有项目添加到List中,然后将新项目添加到List中,然后绑定到Datagrid。
List<Retrievalinfo> gridInfo = (List<Retrievalinfo>)Movie_DataGrid.ItemsSource;
将新项目添加到此列表
gridInfo.Add(new Retrievalinfo());
然后将List绑定到datagrid,
Movie_DataGrid.ItemsSource = gridInfo;