从不同的表单更新 XamDataGrid 源的最佳方法是什么?

What is the best way to update the source of a XamDataGrid from a different form?

我的 MainWindow 中有一个 XamDataGrid,它有一个 Public Shared List(Of Artikelstammdaten) 作为 DataSource。打开其他几个表格后,我想通过单击按钮将更多数据添加到 XamDataGrid。我认为最简单的方法就是更新 DataSource,但我得到一个错误:

The reference to an unreleased member requires an object reference.

这是我试过的:

    Private Sub Add_Click(sender As Object, e As RoutedEventArgs)
        Dim update = MainWindow.listArtikelstammdaten.Concat(CType(Import.ComparedAccessData, IEnumerable(Of Artikelstammdaten)))
        dgArticleMasterData.DataSource = update
        Me.Close()
    End Sub

如果 dgArticleMasterDataMainWindow class 中定义,您需要获取对 MainWindow 实例的引用才能访问它。

您应该可以在 Application.Current.Windows collection:

中找到它
Private Sub Add_Click(sender As Object, e As RoutedEventArgs)
    Dim update = MainWindow.listArtikelstammdaten.Concat(CType(Import.ComparedAccessData, IEnumerable(Of Artikelstammdaten)))

    Dim window = Application.Current.Windows.OfType(Of MainWindow).FirstOrDefault()
    If window IsNot Nothing Then
        window.dgArticleMasterData.DataSource = update
    End If
    Me.Close()
End Sub