Reputation: 6626
I have two windows win1
for display a list of Users and the other win2
for adding user.
I also have a method to refresh the grid after deleted or updated or added user. This method is in win1
.
How do I call this method from win2
after add user?
Upvotes: 2
Views: 4176
Reputation: 91
If you are opening Window 2 from Window 1, you could do something like:
// code in Window1
public void AddNewUser()
{
Window2 window = new Window2();
if (window.ShowDialog() == true)
{
// Update DataGrid
RefreshDataGrid();
}
}
Upvotes: 0
Reputation: 4995
You could use a custom event in your child window that the parent
window listens to
You could define a delegate in the child window that references a method in the parent window
You could use a messenger of some form: Here is a sample:
http://blog.galasoft.ch/archive/2009/09/27/mvvm-light-toolkit-messenger-v2-beta.aspx
Upvotes: 1
Reputation: 15247
This is a pretty basic Object Oriented Design question. So you want to be able to call back from win2
to a function in win1
after a user is added on win2
. Well, does win2
have a reference to the win1
object? If so, then that's how you call it. If not, then you need to give it one (passing it in to the constructor or something).
Alternatively, if you're using an MVVM framework, you could go down the route of using a Messenger. Most of the MVVM frameworks have one included and they're very useful. I recommend going that route unless this is some sort of throwaway program where the plumbing wouldn't be worth it.
Upvotes: 2