开发者

How to show other window from viewmodel?

开发者 https://www.devze.com 2023-04-11 06:26 出处:网络
I have a simple sample thatmy sample has 2 window : 1-ProductlistView 2-ProductEditView (1-ProductlistViewModel 2-ProductEditViewModel)

I have a simple sample that my sample has 2 window : 1-ProductlistView 2-ProductEditView (1-ProductlistViewModel 2-ProductEditViewModel) I want the user can select a product in my ProductlistView and edit selected product in ProductEditView ...i'm using from this code in my sample:

   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();
            }
    }

and write to my editCommant in ProductlistViewModel:

  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();
       ...
        }

I was wondering my sample is wrong? Can i have an instance from a view in its viewmodel? I开发者_StackOverflow中文版f my Sample is wrong how can i do this solution(send an object to other window and after edit get it)?


It is normally recommended to NOT have a ViewModel referencing a View. See this question on how to show a dialog from ViewModel.

0

精彩评论

暂无评论...
验证码 换一张
取 消