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.
精彩评论