I have a Button and I bind this button to a command in ViewModel say OpenWindowCommand
. When I click on the button I want to open a new window. But creating a window instance and showing a window from view model is a violation of MVVM. I have created interface like
interface IWindowService
{
void showWindow(object dataContext);
}
and WindowService
implements this interface like
class WindowService : IWindowService
{
public void showWindow(object dataContext)
{
ChildWindow window=new ChildWindow();
window.DataContext=dataContext;
window.Show();
}
}
In this class I have specified ChildWindow
. So this class is tightly coupled with showing ChildWindow
. When I want to show another window, I have to implement another class with the same interface and logic. How can I make this class generic so that I can just pass an instance of any window and the class will be able to open any window?
I am not using any built MVVM frameworks. I have read many articles on StackOverflow but I could not found any solution for this.