-1
我的主窗口中有一个public void grid_refresh()
方法,它刷新我的DataGrid(主窗口的DataGrid)。现在我有另一个类(Window),它向Datagrid添加了新的元素。我没有做的事情是访问这个grid_refresh
方法刷新网格与新的条目,当我点击我的另一个窗口(添加窗口)的添加按钮。在这个其他类中执行其他类的方法wpf
主窗口代码:
方法来开拓AddBook窗口:
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);
}
}
我想访问的方法:
public void liste_aktualisieren()
的窗口代码我想访问方法来自:
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.
}
}
我真的不知道如何解决这个问题? 我的目的是刷新DataGrid中的btn_add_Click按下
这将通过使用MVVM和一个'ObservableCollection'很容易解决 – BradleyDotNET
您有很多选项。从字面上理解你的问题,在单击按钮时引发的'addBuch'增加一个'event'是最好的。在你的发布代码中,你有一个'mainWindow'字段,但从不初始化它;目前还不清楚为什么你的代码是这样的,但当然如果你想初始化这个字段,你可以用它直接调用这个方法。这种耦合是不好的,但如果你愿意,你可以做到。您也可以使用MVVM并将VM或集合传递给'addBuch'构造函数,以便构造函数可以直接更新集合或调用一个'ICommand'来实现。 –