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

I have a XamDataGrid in my MainWindow which has a Public Shared List(Of Artikelstammdaten) as DataSource. After opening a few other forms I want to add more data to the XamDataGrid with a button click. I thought the easiest way would be just to update the DataSource, but I get an Error:

The reference to an unreleased member requires an object reference.

This is what I have tried:

    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


Solution 1:[1]

If dgArticleMasterData is defined in the MainWindow class, you need to get a reference to the MainWindow instance to be able to access it.

You should be able to find it in the 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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mm8