C# 使用MVVM打开具有相同ViewModel的多个窗口
此问题与此链接相关。C# 使用MVVM打开具有相同ViewModel的多个窗口,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,此问题与此链接相关。 我想在一些服务中打开新窗口。 下面是我的代码 这是我从ViewModel调用的窗口服务 public class WindowService : IWindowService { public void ShowWindow(object viewModel) { var win = new Window {Content = viewModel}; win.Show(); } } 以下是我的App.xaml代码
我想在一些服务中打开新窗口。 下面是我的代码 这是我从ViewModel调用的窗口服务
public class WindowService : IWindowService
{
public void ShowWindow(object viewModel)
{
var win = new Window {Content = viewModel};
win.Show();
}
}
以下是我的App.xaml代码
<DataTemplate DataType="{x:Type viewModel:MainViewModel}" >
<viewModel:ChildWindow />
</DataTemplate>
现在,这适用于具有不同ViewModel的所有窗口。
但当我想打开另一个使用相同视图模型但视图不相同的窗口时,我无法在App.xaml中定义相同类型的数据模板
如何打开具有相同ViewModel的多个新窗口?
我应该为每个窗口创建不同的ViewModel吗?我已经展示了打开和关闭窗口的另一种方法 您可以将
dependencProperty
添加到OpenCloseWindowBehavior
中,使用它传递ViewModel,并在行为代码中将窗口的DataContext
连接到此ViewModel。如果你需要更多的帮助,告诉我
<local:OpenCloseWindowBehavior WindowType="local:YellowWindow" Open="{Binding YellowOpen, Mode=TwoWay}" DataContext="{Binding SomeViewModel}" />