如何将一个窗口设置为从 ViewModel 声明、初始化和打开的所有者?
代码如下:
public class ViewModel : INotifyPropertyChanged
{
// declaration
static nextWindow nw;
...
public ICommand OpenNextWindow { get { return new RelayCommand(OpenNextWindowExecute, CanOpenNextWindowExecute); } }
bool CanOpenNextWindowExecute(object parameter)
{
return true;
}
void OpenNextWindowExecute(object parameter)
{
nw = new nextWindow();
nw.WindowStartupLocation = WindowStartupLocation.CenterScreen;
// Set this window as owner before showing it...
nw.Show();
}
}
在 nextWindow 的代码隐藏文件中,我可以使用以下代码将 nextWindow 设置为所有者:
nw.Owner = this;
如何从 View 模型中实现它?
最佳答案
老实说,我不会做任何与在 ViewModel 中显示窗口相关的事情。不过,您可以做的是向 View 发送消息(例如使用 MVVMLight 的 Messenger 服务),您可以在 View 中进行显示并设置所有者 shinanigaz
关于c# - 如何在 WPF 中将窗口设置为 ViewModel 的所有者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40786442/