Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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
如何使wpf工具包datagrid在绑定到数据集时显示新行_Wpf_Data Binding_Datagrid_Wpftoolkit - Fatal编程技术网

如何使wpf工具包datagrid在绑定到数据集时显示新行

如何使wpf工具包datagrid在绑定到数据集时显示新行,wpf,data-binding,datagrid,wpftoolkit,Wpf,Data Binding,Datagrid,Wpftoolkit,有没有办法让wpf toolkitDataGrid在绑定到数据集时显示新行?换句话说,我有一个DataGrid,我已经将它的itemsource设置为DataTable,一切似乎都很正常,只是我无法让网格显示我通过编程方式添加到DataTable的行。您可以将DataGrid.itemsource设置为observeablecollection observetecollection items=新的observetecollection(); yourDataGrid.ItemsSource=

有没有办法让wpf toolkit
DataGrid
在绑定到
数据集时显示新行?换句话说,我有一个
DataGrid
,我已经将它的
itemsource
设置为
DataTable
,一切似乎都很正常,只是我无法让网格显示我通过编程方式添加到
DataTable
的行。

您可以将
DataGrid.itemsource
设置为
observeablecollection

observetecollection items=新的observetecollection();
yourDataGrid.ItemsSource=项目;
然后,您应该能够添加到集合中,以显示新行:

编辑:基于更新的信息。

if (Dispatcher.CheckAcces())
{
    // already on thread UI control was created on
    items.Add(<your item>);
}
else
{
    // update on same thread UI control was created on
    // BeginInvoke would invoke a delegate which would call items.Add(<your item>)
    Dispatcher.BeginInvoke(...);
}
if(Dispatcher.CheckAcces())
{
//已在线程上创建UI控件
items.Add();
}
其他的
{
//在上创建的同一线程UI控件上更新
//BeginInvoke将调用调用items.Add()的委托
调度员开始发号施令(…);
}

看。所有对象都有一个
Dispatcher
属性。

我不知道有人会怎么想,特别是因为我没有提到它,但问题是我是从窗体主线程以外的线程(即创建网格的线程)更新数据集的。您可以从另一个线程进行更新,但不能进行插入,尽管我不知道为什么。如果有人能解释一下,我会很感激的。我猜网格会检查插入是否在另一个线程上,如果是,它会忽略它,因为这会导致异常。

谢谢您的建议。我已经尝试过了,当我使用一个可观察的集合时,我的网格中的所有数据都是空白的,我仍然有同样的问题。您添加到可观察集合的项是否实现了InotifyProperty更改?YourItem中的属性需要与XAML中配置的属性相匹配。也许如果你发布你的代码,我可以帮上更多的忙。你应该总是从创建控件的线程中更新控件。这并不特定于DataGrid。但是更新控件是否扩展到更新其绑定的数据?当我使用Xeed网格时,这不是一个问题,尽管Xeed可能会在幕后处理它。
if (Dispatcher.CheckAcces())
{
    // already on thread UI control was created on
    items.Add(<your item>);
}
else
{
    // update on same thread UI control was created on
    // BeginInvoke would invoke a delegate which would call items.Add(<your item>)
    Dispatcher.BeginInvoke(...);
}