将行添加到数据网格时,C#WPF抛出System.InvalidOperationException
我有一个WPF数据网格,希望用代码向其中添加数据 XAML: XAML.cs:将行添加到数据网格时,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.
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" });
}));