including the WPF Window

时间:2021-11-08 04:51:30

including the WPF Window

main window预览:

Model: 

including the WPF Window

including the WPF Window

view model1:

including the WPF Window

viewmodel2: CustomerInfoViewModel

including the WPF Window

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

including the WPF Window

创建一个view:  CustomeInfoView

including the WPF Window

including the WPF Window

看一下父子窗体:

including the WPF Window

疑问: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:

including the WPF Window

如果将childviewmodel.Info文字变动成如下逻辑,,效果:

including the WPF Window

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

including the WPF Window

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