Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.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抛出System.InvalidOperationException_C#_Wpf_Xaml_Datagrid - Fatal编程技术网

将行添加到数据网格时,C#WPF抛出System.InvalidOperationException

将行添加到数据网格时,C#WPF抛出System.InvalidOperationException,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,我有一个WPF数据网格,希望用代码向其中添加数据 XAML: XAML.cs: WareneingangEinbuchenGrid.Items.Add(new WareneingangGridData { Name = "Testname", Stueckzahl = 2, Preis = 12.3 , Barcode = "TestBarcode" }); 执行XAML.CS时,应用程序抛出System.InvalidOperationException错误。找到解决方案,我编辑了XAML.

我有一个WPF数据网格,希望用代码向其中添加数据

XAML:

XAML.cs:

WareneingangEinbuchenGrid.Items.Add(new WareneingangGridData { Name = "Testname", Stueckzahl = 2, Preis = 12.3 , Barcode = "TestBarcode" });

执行XAML.CS时,应用程序抛出System.InvalidOperationException错误。

找到解决方案,我编辑了XAML.CS:

Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
        {
            WareneingangEinbuchenGrid.Items.Add(new WareneingangGridData { Name = "Testname", Stueckzahl = 2, Preis = 12.3, Barcode = "TestBarcode" });
        }));

我觉得不错。除了您可能应该设置ItemSource之外。而不是项目。添加
WareneingangEinbuchenGrid.Items.Add(new WareneingangGridData { Name = "Testname", Stueckzahl = 2, Preis = 12.3 , Barcode = "TestBarcode" });
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
        {
            WareneingangEinbuchenGrid.Items.Add(new WareneingangGridData { Name = "Testname", Stueckzahl = 2, Preis = 12.3, Barcode = "TestBarcode" });
        }));