WPF MVVC 基础 - 父子窗体

Posted One is never too old to learn.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF MVVC 基础 - 父子窗体相关的知识,希望对你有一定的参考价值。

结构: 

 

 main window预览:

Model: 

view model1:

 viewmodel2: CustomerInfoViewModel

其中上方缩小部分的代码如下:

 

创建一个view:  CustomeInfoView

看一下父子窗体:

 

 疑问:DataContext指的是?

The DataContext property is the default source of your bindings, unless you specifically declare another source, like we did in the previous chapter with the ElementName property. It\'s defined on the FrameworkElement class, which most UI controls, including the WPF Window, inherits from. Simply put, it allows you to specify a basis for your bindings

There\'s no default source for the DataContext property (it\'s simply null from the start), but since a DataContext is inherited down through the control hierarchy, you can set a DataContext for the Window itself and then use it throughout all of the child controls. Let\'s try illustrating that with a simple example:

 

如果将childviewmodel.Info文字更改成如下逻辑,效果:

 我们也可以将viewmodel拓展成属性property.

 

设置为public的话,外部也可以调用了。

以上是关于WPF MVVC 基础 - 父子窗体的主要内容,如果未能解决你的问题,请参考以下文章

WPF 窗体在Alt+Tab中隐藏

新手求教WPF中如何设置打开子窗体后父窗体关闭或隐藏.

C#父子窗体怎么弄

WPF 之 设置Dialog的父窗体

WPF提示窗口总在主窗体前是怎么实现的

C# WPF MVVM模式下在主窗体显示子窗体并获取结果