在WPF中向DataGrid添加行? [英] Adding A Row To A DataGrid In WPF?

查看:504
本文介绍了在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.

这篇关于在WPF中向DataGrid添加行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆