wpf 在视图模型之间传递参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19513555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing parameters between viewmodels
提问by Chris
In my app I have 2 views. List(GridView) and Form. I am changing views as proposed in this thread: WPF MVVM switch usercontrols
在我的应用程序中,我有 2 个视图。列表(GridView)和表单。我正在按照此线程中的建议更改视图: WPF MVVM switch usercontrols
Now I have a problem how to pass id of selected item after click edit to show new View with the edit form.
现在我遇到了一个问题,如何在单击编辑后传递所选项目的 id 以显示带有编辑表单的新视图。
Simple application to list all items, add new, delete and edit. How can I do this in MVVWM?
简单的应用程序来列出所有项目,添加新的,删除和编辑。我怎样才能在 MVVWM 中做到这一点?
UPDATE更新
I just want to create a simple application, that has menu on the left with:
我只想创建一个简单的应用程序,它的左侧有菜单:
- List
- Add new.
- 列表
- 添新。
When click List, it shows UC with list and 3 buttons: Add, Edit, Delete. After click add, edit it shows UC with Form with specific values (while editing). How can I achieve this??
单击列表时,它会显示带有列表和 3 个按钮的 UC:添加、编辑、删除。单击添加后,编辑它会显示带有特定值的表单的 UC(编辑时)。我怎样才能做到这一点?
回答by JoanComasFdz
As far as I understand, you want something like this:
据我了解,你想要这样的东西:
So that when you click Add, it shows this:

因此,当您单击 时Add,它会显示以下内容:

Right?
对?
So you need the following behaviour:
所以你需要以下行为:
- Add doesn't needs any ID.
- List must reload after Add finishes.
- Edit receives the list's selected item ID .
- List must reload after Add finishes.
- 添加不需要任何 ID。
- 添加完成后必须重新加载列表。
- Edit 接收列表的选定项 ID 。
- 添加完成后必须重新加载列表。
I will assume that you are working with some repository.
我将假设您正在使用某个存储库。
I propose the following MVVM structure:
我提出以下 MVVM 结构:
MainViewModel: The DataContext for the main screen.BaseViewModel RightViewModel: The container for viewmodels shown on the right part of the screen.ICommand ViewListCommand: Shows the list by creating a new instance ofListViewModeland assigning it to theBaseViewModelproperty.ICommand AddNewCommand: Shows the addnew screen by creating a new isntance ofAddViewModeland assigning it to theBaseViewModelproperty.
MainViewModel:主屏幕的DataContext。BaseViewModel RightViewModel:显示在屏幕右侧的视图模型的容器。ICommand ViewListCommand:通过创建 的新实例ListViewModel并将其分配给BaseViewModel属性来显示列表。ICommand AddNewCommand:通过创建一个新的实例AddViewModel并将其分配给BaseViewModel属性来显示 addnew 屏幕。
Then:
然后:
ListViewModel: The DataContext for the right part of the screen when List is clicked.List<Items> Items: Provides the items to be shown.Item SelectedItem: The property that will be binded to the SelectedItem on the UI.ICommand EditCommand: The command that will get the selected item and notify the MainViewModel that has to be edited.
ListViewModel:单击 List 时屏幕右侧部分的 DataContext。List<Items> Items:提供要显示的项目。Item SelectedItem:将绑定到 UI 上 SelectedItem 的属性。ICommand EditCommand: 该命令将获取所选项目并通知必须编辑的 MainViewModel。
So at some point, a viewmodel will receive a notification from a child viewmodel (i.e. list will tell main to edit somethin). I recommend to do this by using events as:
因此,在某些时候,视图模型会收到来自子视图模型的通知(即列表将告诉主编辑某些内容)。我建议通过使用事件来做到这一点:
public class Item
{
public string Name { get; set; }
}
public class ItemEventArgs : EventArgs
{
public Item Item { get; set; }
public ItemEventArgs(Item selectedItem)
{
this.Item = selectedItem;
}
}
public class BaseViewModel
{
}
public class ListViewModel : BaseViewModel
{
public event EventHandler<ItemEventArgs> EditItem;
public Item SelectedItem { get; set; }
public ICommand EditItemCommand { get; private set; }
public ListViewModel()
{
this.EditItemCommand = new DelegateCommand(() => this.EditItem(this, new ItemEventArgs(this.SelectedItem)));
}
}
public class EditViewModel : BaseViewModel
{
}
public class MainViewModel
{
public BaseViewModel RightViewModel { get; private set; }
public ICommand ViewListCommand { get; private set; }
public MainViewModel()
{
this.ViewListCommand = new DelegateCommand(() =>
{
// unhook possible previous events
var listViewModel = new ListViewModel();
listViewModel.EditItem += listViewModel_EditItem;
this.RightViewModel = listViewModel;
});
}
void listViewModel_EditItem(object sender, ItemEventArgs e)
{
// unhook possible previous events
var editViewModel = new EditViewModel();
this.RightViewModel = editViewModel;
}
}
The xaml binding is not necessary since it will be quite forward.
xaml 绑定不是必需的,因为它将非常向前。
This is a very basic example about how I think this kind of stuff can be handled. The important thing here is to properly unhook the events, otherwise you can run into problems.
这是一个关于我认为如何处理此类事情的非常基本的例子。这里重要的是正确解除事件的挂钩,否则您可能会遇到问题。
For this kind of stuff you can also have a look at Reactive UI.
对于此类内容,您还可以查看Reactive UI。
回答by Sheridan
If you have a common parent of the view models, then you can use that parent to pass parameter values for you. Simply set up one or more delegates in the relevant view models:
如果您有视图模型的公共父级,那么您可以使用该父级为您传递参数值。只需delegate在相关视图模型中设置一个或多个:
In the view model with the relevant parameter to update... let's call it ParameterViewModel:
在带有要更新的相关参数的视图模型中......让我们称之为ParameterViewModel:
public delegate void ParameterChange(string parameter);
public ParameterChange OnParameterChange { get; set; }
In the parent:
在父级中:
ParameterViewModel viewModel = new ParameterViewModel();
viewModel.OnParameterChange += ParameterViewModel_OnParameterChange;
ListMenu.Add(viewModel);
// Add other view models
Back in ParameterViewModelwhen the parameter changes:
ParameterViewModel当参数改变时返回:
public string Parameter
{
get { return parameter; }
set
{
parameter = value;
NotifyPropertyChanged("Parameter");
// Always check for null
if (OnParameterChange != null) OnParameterChange(parameter);
}
}
Now in the parent view model:
现在在父视图模型中:
public void ParameterViewModel_OnParameterChange(string parameter)
{
// Do something with the new parameter data here
AnotherViewModel anotherViewModel = (AnotherViewModel)ListMenu[someIndex];
anotherViewModel.Parameter = parameter;
}
You can find out more about using delegateobjects from the Delegates (C# Programming Guide)page on MSDN.
您可以delegate从MSDN 上的委托(C# 编程指南)页面找到有关使用对象的更多信息。

