How do I let the view know that it needs to close the window?

advertisements

I am using MVVM, from one I know the commnication for Data is View <=> ViewModel <=> Model.

The ViewModel can interact with View by using Two-Way binding.

But now I have a LoginView (which is in a Window), if the login successful will be check in ViewModel.

When it fail, it should tell the Window to close. But...how? The ViewModel is not suppose to know about the View....


Your ViewModel is a representation of your UI state. You could simply have a IsLoginWindowVisible boolean property exposed, your view can then have code (yes, I said it, view can have code!) that shows / hides a windows based on the state of this property.

I think people stress too much about MVVM. As long as your ViewModel can execute without a view present, in order to facilitate testing, you are doing MVVM correctly. There is nothing wrong with having code to support your view.