如何将窗口设置为从ViewModel声明,初始化并打开的所有者?如何从WPF中的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;
如何从视图模型实现的呢?
你可以尝试结合它到您ViewModel中的一个Window,并且从您VM中将Window设置为任何相关的。 但是,你通常希望你ViewModel不知道你的视图,所以我不知道这将是一个干净的方式。 – Belterius