0

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? If my Sample is wrong how can i do this solution(send an object to other window and after edit get it)?

M.Azad
  • 3,673
  • 8
  • 47
  • 77

1 Answers1

1

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

Community
  • 1
  • 1
Jake Berger
  • 5,237
  • 1
  • 28
  • 22
  • I see this solution in this this Architecture :http://www.codeproject.com/KB/architecture/wcfbyexample_introduction.aspx – M.Azad Oct 08 '11 at 07:30
  • Yes, some frameworks take a "View-first" approach, while others (like the one in your link) take a "ViewModel-first" approach. It's your decision. [Google Query](http://www.google.com/search?q=mvvm+view+first+vs+viewmodel+first). If you want an example for MVVM Light using View-first, I can update my answer. – Jake Berger Oct 08 '11 at 23:16