c# - 如何在 WPF 中将窗口设置为 ViewModel 的所有者

标签 c# wpf mvvm

如何将一个窗口设置为从 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/

相关文章:

c# - Wpf MVVM在DataGrid中选择/取消选择项目

c# - ListView不会删除Xamarin.Forms中的项目。我已将ObservableCollection分配给ListView itemsource。 MVVM

wpf - 如何从 ViewModel 访问 MetroWindow 的 ShowMessageAsync 方法

c# - 是否有 WCF 服务请求队列性能计数器?

c# - 使用设置文件时出错。

c# - 在列表框中选择一个目录并在另一个列表框中显示所选目录的文件

c# - 位图插值

.net - 在WPF中使用OpenFileDialog时没有文件的文件夹路径?

wpf - 无法更新 DataGrid

.net - ViewModel 类的可重用性如何?