-1

I do have a public void grid_refresh() Method in my Main Window which refreshes my DataGrid (Main Window's DataGrid). Now I do have another class (Window) which adds new elements to the Datagrid. The thing I don't get done is to access this grid_refresh Method to refresh the grid with the new entries when I hit the Add Button from my other window (Add Window).

Main Window Code:

Method to open up the AddBook Window:

        private void AddBuch(object sender, RoutedEventArgs e)
    {
        if (Title == "Dictionary")
        {
            MessageBox.Show("Wählen Sie zuerst unter Ansicht eine Kategorie aus!");
        }
        else
        {
            addBuch addbuch = new addBuch(this);
            addbuch.Show();
            //do { } while (addbuch.ShowDialog() == true);
        }
    }

The method I want to get access to:

public void liste_aktualisieren()

The Window Code I want to access the Method from:

    public partial class addBuch : Window
{
    private MainWindow mainWindow;

    public addBuch(Window owner)
    {
        InitializeComponent();
        Owner = owner;
        SetProperties();
        owner.IsEnabled = false;
    }
    private void btn_add_Click(object sender, RoutedEventArgs e)
    {
        if (txt_name.Text == "" | txt_isbn.Text == "" | txt_datum.Text == "" | txt_genre.Text == "" | txt_autor.Text == "" | txt_seiten.Text == "")
        {
            MessageBox.Show("Es sollte allen Feldern ein Wert zugewiesen werden.\nVersuchen Sie es erneut!");
        }
        else
        {
            cDictionary.Buch_hinzufuegen(txt_name.Text, txt_isbn.Text, txt_datum.Text, txt_genre.Text, txt_autor.Text, Convert.ToInt32(txt_seiten.Text));
            Owner.IsEnabled = true;

            //Somewhere here I want to Acess the DataGrid Refresh Method so the Datagrid in the Main Window Refreshes.
        }
    }

I am not really sure how to get around this... My intention is to refresh the Datagrid the Moment the btn_add_Click is pressed

Niclas
  • 25
  • 1
  • 1
  • 6
  • This would be solved by using MVVM and an `ObservableCollection` quite easily – BradleyDotNET Jun 16 '17 at 00:19
  • You have many options. Taking your question literally, adding an `event` to `addBuch` which is raised when the button is clicked, is best. In your posted code, you have a `mainWindow` field but never initialize it; it's not clear why your code is like that, but of course if you'd initialize that field, you could use it to call the method directly. This kind of coupling is bad, but you can do it if you want. You could also use MVVM and pass the VM or collection to the `addBuch` constructor, so that the constructor can either update the collection directly or call an `ICommand` that does so. – Peter Duniho Jun 16 '17 at 22:04

1 Answers1

0

Since you are injecting your addBuch window with a reference to the MainWindow, you could call the method through this reference:

public partial class addBuch : Window
{
    private MainWindow mainWindow;

    public addBuch(MainWindow owner) //<--
    {
        InitializeComponent();
        Owner = owner;
        SetProperties();
        owner.IsEnabled = false;
        mainWindow = owner; //<---
    }

    private void btn_add_Click(object sender, RoutedEventArgs e)
    {
        //...
        mainWindow.grid_refresh();
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88