C# View 如何知道在 WPF 中使用什么 ViewModel?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11141692/
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
How does a View know what ViewModel to use in WPF?
提问by Bob.
Can someone explain how the View and ViewModel are connected? I can't find anywhere the xaml or the xaml.cs for the View that references the ViewModel, nor anything in the ViewModel.cs file that references the View, yet they are somehow connected, and binding members from the ViewModel to the View work.
有人可以解释一下 View 和 ViewModel 是如何连接的吗?我在任何地方都找不到引用 ViewModel 的视图的 xaml 或 xaml.cs,也找不到引用视图的 ViewModel.cs 文件中的任何内容,但它们以某种方式连接,并将成员从 ViewModel 绑定到视图工作.
Also, in the constructor of each, there is only the InitializeComponent for the View and a basic constructor for the ViewModel (no declaration/definition of the View).
此外,在每个构造函数中,只有视图的 InitializeComponent 和视图模型的基本构造函数(没有视图的声明/定义)。
Thanks!
谢谢!
采纳答案by Reed Copsey
There are various options here.
这里有多种选择。
Somethinghas to set the View's DataContextto be an instance of the ViewModel. There are lots of options here:
一些具有设置视图的DataContext是视图模型的实例。这里有很多选择:
- This can be done directly in xaml (the View just instances the ViewModel directly).
- This can be done in the View's constructor (
this.DataContext = new MyViewModel();) - This can be handled via a
DataTemplate - A "coordinating" class can wire these together (ie: a separate "presenter" class can construct both and set the
DataContextappropriately)
- 这可以直接在 xaml 中完成(View 只是直接实例化 ViewModel)。
- 这可以在 View 的构造函数 (
this.DataContext = new MyViewModel();) 中完成 - 这可以通过
DataTemplate - “协调”类可以将它们连接在一起(即:单独的“演示者”类可以构建两者并
DataContext适当地设置)
The most common are to either have the View define the VM in the xaml (View-first), or to have everything based from a ViewModel-centric point of view, and have WPF automatically create the View based on the bound VM (ViewModel-first).
最常见的方法是让 View 在 xaml 中定义 VM(View-first),或者让一切都基于以 ViewModel 为中心的观点,并让 WPF 基于绑定的 VM(ViewModel-第一的)。
The former approach is what's used by a lot of toolkits, such as MVVM Light. The latter approach is what I used in my MVVM blog series, and used by some other toolkits.
前一种方法被许多工具包使用,例如MVVM Light。后一种方法是我在我的MVVM 博客系列中使用的,并被其他一些工具包使用。
回答by Justin
The view contains an object of the view model class in the xaml.
该视图包含 xaml 中视图模型类的对象。
The InitializeComponent function creates all the controls on the page, sets styles, etc.
InitializeComponent 函数创建页面上的所有控件、设置样式等。
回答by Danny Varod
A "clean" way for connecting the views to the view-models would be...
将视图连接到视图模型的“干净”方式是......
When you create the views, for each view, set its DataSource to its view-model:
创建视图时,对于每个视图,将其 DataSource 设置为其视图模型:
E.g.
例如
public class App
{
private void OnAppStart()
{
var model = new MainModel();
var vm = new MainVM();
var view = new MainWindow();
vm.Model = model;
view.DataSource = vm;
view.Show();
}
}
When the model you are viewing changes, update the VM:
当您查看的模型发生变化时,更新 VM:
public class MainVM
{
private void OnSelectedModelItemChanged()
{
this.SelectedItem = new ItemVM();
this.SelectedItem.Model = this.SelectedModelItem;
}
}
And use data templates to make view select the correct sub views for each VM.
并使用数据模板使视图为每个 VM 选择正确的子视图。

