简体   繁体   中英

WPF Grid not showing content

i´m using a Window with a Grid wich loads Objects from a MongoDB. This Objects contains Lists. Now i want to Load a List from one of this Objects. This is the Source from the Window:

        public void btn_load_Click(object sender, RoutedEventArgs e)
    {
        MainWindow M = new MainWindow();

        ArtikelLaden();
        M.RefreshGrid();

        Close();

    }`        
public void ArtikelLaden()
    {

        MainWindow M = new MainWindow();
        rowindexArtikel = dg_Artikel.SelectedIndex;
        Artikel B = new Artikel();
        B = artikelList[rowindexArtikel];
        M.loadArtikel(B);
    }

The List should get loaded in the MainWindow:`

public void loadArtikel(Artikel B)
    {
        dg_test.ItemsSource = null;
        dg_test.ItemsSource = B.kaufList;
        dg_test.Items.Refresh();
        MessageBox.Show(dg_test.HasItems.ToString());
        MessageBox.Show(dg_test.Items.Count.ToString());

        dg_Teile.ItemsSource = null;
        dg_Teile.ItemsSource =B.teilList;
        dg_Teile.Items.Refresh();

        //dg_Teile.Items.Refresh();
        //dg_BauGrp.ItemsSource = B.bauList ;
    }
    public void RefreshGrid()
    {
        //dg_Kaufteile.UpdateLayout();
        //dg_Kaufteile.Items.Refresh();
        //dg_Teile.UpdateLayout();
        MessageBox.Show(dg_Kaufteile.HasItems.ToString());
        MessageBox.Show(dg_Kaufteile.Items.Count.ToString());
    }

When u see some curios things in the source is just because i test now thousands of possibilities. But the Grids in the MainWindow always simply stays empty.

The first MsgBox says that the grid has Content. Later in second MsgBox in the Refresh Method it says its empty?!

When i load the Lists in the first Window it works. The Grids have AutoColumn enabled.

`

You are creating a new instance of MainWindow but you probably want to access the already existing one that you see on the screen. Try this:

public void btn_load_Click(object sender, RoutedEventArgs e)
{
    MainWindow M = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();

    ArtikelLaden(M);
    M.RefreshGrid();

    Close();
}

public void ArtikelLaden(MainWindow M)
{
    rowindexArtikel = dg_Artikel.SelectedIndex;
    Artikel B = new Artikel();
    B = artikelList[rowindexArtikel];
    M.loadArtikel(B);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM