Reputation: 1221
This question is in reference to this link Opening new window in MVVM WPF.
I want to open new window in some service.
Following is my code
This is a window service which I am calling from ViewModel
public class WindowService : IWindowService
{
public void ShowWindow(object viewModel)
{
var win = new Window {Content = viewModel};
win.Show();
}
}
Following is my App.xaml code
<DataTemplate DataType="{x:Type viewModel:MainViewModel}" >
<viewModel:ChildWindow />
</DataTemplate>
Now this works fine for all windows which have different ViewModel. But when I want to open another window which is using same view model but their view is not same,I can not define data template of same type in App.xaml.
How do I open multiple new window which have same ViewModel? Should I create different ViewModel for each window?
Upvotes: 0
Views: 1419
Reputation: 12319
I have shown an alternative way of opening and closing windows here.
You could add a DependencyProperty
e.g. DataContext
to the OpenCloseWindowBehavior
, use it to pass the ViewModel, and hook up the window's DataContext
to this ViewModel in the code of the behavior. Tell me if you need more help.
<local:OpenCloseWindowBehavior WindowType="local:YellowWindow" Open="{Binding YellowOpen, Mode=TwoWay}" DataContext="{Binding SomeViewModel}" />
Upvotes: 1