【问题标题】:How to show other window from viewmodel?如何从视图模型显示其他窗口?
【发布时间】:2011-10-07 17:02:13
【问题描述】:

我有一个简单的示例,我的示例有 2 个窗口: 1-ProductlistView 2-ProductEditView(1-ProductlistViewModel 2-ProductEditViewModel) 我希望用户可以在我的 ProductlistView 中选择一个产品并在 ProductEditView 中编辑选定的产品...我在我的示例中使用此代码:

   public Class   ProductEditViewModel:ViewModelBase 
    {
        private readonly ProductEditView View;
        public ProductModel Model { get; set; }
        public ProductEditViewModel(Product myproduct)
        {
            View = new ProductEditView { DataContext = this };
            if(myproduct!= null) Model  = myproduct;

        }
         private bool IsInDialogMode;
            public bool? ShowDialog()
            {
                if (IsInDialogMode) return null;
                IsInDialogMode = true;
                return View.ShowDialog();
            }
    }

并写信给我在 ProductlistViewModel 中的 editCommant:

  private RelayCommand UpdateProductmdInstance;
   public RelayCommand UpdateProductCommand
        {
            get
            {
                if (UpdateProductmdInstance!= null) return UpdateProductmdInstance;
                UpdateProductmdInstance= new RelayCommand(a => OpenProductDetail(SelectedProduct), p => SelectedProduct!= null);
                return UpdateProductmdInstance;
            }
        }

        private void OpenProductDetail(Product product)
        {
            var ProductEditViewModel= new ProductEditViewModel(product);
            var result = personDetailViewModel.ShowDialog();
       ...
        }

我想知道我的样本有误? 我可以从其视图模型中的视图中获得一个实例吗? 如果我的示例错误,我该如何解决(将对象发送到其他窗口并在编辑后获取它)?

【问题讨论】:

    标签: c# .net wpf mvvm


    【解决方案1】:

    通常建议不要让 ViewModel 引用 View。请参阅this question,了解如何从 ViewModel 显示对话框。

    【讨论】:

    • 我在这个架构中看到了这个解决方案:codeproject.com/KB/architecture/wcfbyexample_introduction.aspx
    • 是的,一些框架采用“视图优先”的方法,而其他框架(如链接中的那个)采用“视图模型优先”的方法。是你的决定。 Google Query。如果您想要使用 View-first 的 MVVM Light 示例,我可以更新我的答案。
    猜你喜欢
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 2013-11-30
    • 2012-06-18
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    相关资源
    最近更新 更多