Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式向WPF DataGrid添加新行_C#_Wpf_Datagrid_Rows - Fatal编程技术网

C# 以编程方式向WPF DataGrid添加新行

C# 以编程方式向WPF DataGrid添加新行,c#,wpf,datagrid,rows,C#,Wpf,Datagrid,Rows,我试图在单击特定按钮时向WPF应用程序上的DataGrid添加新行。 这就是我迄今为止所尝试的: DataGridRow row = new DataGridRow(); table.Items.Add(row); // table = my DataGrid 当我运行这段代码时,它抛出一个System.ArgumentNullException语句 值不能为空 我该怎么解决这个问题? 提前感谢。您需要将数据网格的项资源属性绑定到POCO对象集合,而不是添加数据网格行,向该集合添加新的POCO

我试图在单击特定按钮时向WPF应用程序上的DataGrid添加新行。 这就是我迄今为止所尝试的:

DataGridRow row = new DataGridRow();
table.Items.Add(row); // table = my DataGrid
当我运行这段代码时,它抛出一个
System.ArgumentNullException
语句

值不能为空

我该怎么解决这个问题?
提前感谢。

您需要将数据网格的
项资源
属性绑定到POCO对象集合,而不是添加数据网格行,向该集合添加新的POCO对象,您的“问题”将自动得到解决


这假设VM属性的更改通知以及在POCO类中实现INotifyPropertyChanged当然…

这应该是绑定的…向网格绑定的集合添加数据项因为您考虑的是紧密耦合的视图,将您的想法改为使用基于集合项自动创建行的模板的视图,而不添加显式行。