如何从视图模型显示其他窗口?
我有一个简单的示例,我的示例有 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();
...
}
我想知道我的示例是错误的? 我可以在其视图模型中的视图中有一个实例吗? 如果我的示例是错误的,我该如何执行此解决方案(将对象发送到其他窗口并在编辑后获取它)?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常建议不要让 ViewModel 引用视图。请参阅此问题了解如何从 ViewModel 显示对话框。
It is normally recommended to NOT have a ViewModel referencing a View. See this question on how to show a dialog from ViewModel.