在WPF中向DataGrid添加行?

问题描述:

因此,在WinForms中,您可以轻松地添加一行,例如

So in WinForms you can easily add a row, for example

dataGridView1.Rows.Add(user.Handle, c);

但是在WPF中,当我尝试使用DataGrid时没有行属性。
是否可以在WPF中执行任何操作,而这不包含疯狂的代码行或XAML造成的混乱?

But in WPF, when I try to use a DataGrid there is no 'Rows' property. Is there any way to do this in WPF that doesn't consist of an insane amount of lines of code or a lot of messing with XAML?

很简单:

// add a row    
DataGrid.Items.Add(new DataItem()); 

// add a column
DataGrid.Columns.Add(new DataGridTextColumn()); 

请参考此链接以获取更多信息, http://wpf.codeplex.com/Thread/View.aspx?ThreadId=34065

Please refer this link for more, http://wpf.codeplex.com/Thread/View.aspx?ThreadId=34065

或者,如果您不想直接添加行,可以使用集合作为源。
将网格绑定到列表(可观察的集合)。将项目添加到该列表。
结果:新行显示在网格中。

Or if you don't like to add rows directly like that, use a collection as source. Bind the Grid to a List (Observable collection). Add items to that list. Result: new rows show up in the grid.