Reputation: 115
I looked around a bit on how to manually trigger a Viewmodel to run again, but wasn't sure they were directed towards what I actually want.
What I want to do is be able to run a specific Method that is inside of my Viewmodel whenever a specific Window is closed, without running all of them. Is there a way that I could bind this Method to the Closed Event of the second Window? Thanks.
Upvotes: 0
Views: 49
Reputation: 145
are you using MVVM Light or any other MVVM framework ?
if so: you have multiple options depending on your design you might want to choose
Upvotes: 0
Reputation: 3925
If you want to stick to MVVM
and do it properly, in a easy to extend and maintain way you can do this like this:
public class FirstViewModel : IWindowCloseNotifier {
public SecondViewModel SecondVm { get; set; }
public FirstViewModel()
{
SecondVm = new SecondViewModel(this);
}
public void Close(IWindowCloseNotifierArgs args)
{
// Window is now closed!
}
}
Interface ensures that SecondViewModel
can use only Close
method.
public interface IWindowCloseNotifier {
void Close(IWindowClosedArgs args);
}
public class SecondViewModel {
private readonly IWindowCloseNotifier _windowCloseNotifier;
public SecondViewModel(IWindowCloseNotifier windowCloseNotifier) {
_windowCloseNotifier = windowCloseNotifier;
}
public void OnClose()
{
_windowCloser.Close(your args);
}
}
If your SecondViewModel
is not a child of FirstViewModel
then use Publish Subscribe Pattern to communicate between viewmodels.
My naming is not great, but I don't have any other ideas.
Upvotes: 1