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的话,外部也可以调用了。

转载于:https://www.cnblogs.com/watermarks/p/8469170.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值