简体   繁体   English

以编程方式将新行添加到WPF DataGrid

[英]Programmatically add new row to WPF DataGrid

I'm trying to add new rows to a DataGrid on a WPF application when clicking on a specific button. 单击特定按钮时,我正在尝试向WPF应用程序上的DataGrid添加新行。 This is what I tried so far: 这是我到目前为止尝试过的:

DataGridRow row = new DataGridRow();
table.Items.Add(row); // table = my DataGrid

When I run this code, it throws a System.ArgumentNullException saying 当我运行此代码时,它将引发System.ArgumentNullException

Value cannot be null 值不能为空

What should I do to solve this problem? 我该怎么做才能解决这个问题? Thanks in advance. 提前致谢。

You need to bind your Data Grid's ItemsSource property to a collection of POCO objects and instead of adding Data Grid rows add new POCO objects to that collection and your "problem" will be automagically resolved... 您需要将Data Grid的ItemsSource属性绑定到POCO对象的集合,而不是添加Data Grid行,而是向该集合添加新的POCO对象,您的“问题”将自动解决。

This assumes change notification of your VM properties and implementing INotifyPropertyChanged in your POCO classes of course... 假设您的VM属性发生更改通知,并且当然在POCO类中实现了INotifyPropertyChanged ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM