首页 技术 正文
技术 2022年11月16日
0 收藏 649 点赞 2,580 浏览 7562 个字

WPF之DataContext(转)

有时候不是你不够聪明,而是别人教给你的东西太烂!相信自己!这是我认为,目前网络上对“DataContext”解释最好的一篇文章,跟大家分享。原文地址:https://rachel53461.wordpress.com/2012/07/14/what-is-this-datacontext-you-speak-of/WPF之DataContext(转)

What is this “DataContext” you speak of?

I frequently see new WPF users confused about what the DataContext is, and how it is used. Hopefully, this can help clarify what the DataContext is, and how it is used.

What is the DataContext?

In WPF, there are two layers to an application: the UI layer and the Data layer.

The Data layer for an application starts out as null, and you can set it using the DataContext property. All UI objects will inherit their DataContext from their parent unless you specify otherwise.

When using the Model-View-ViewModel (MVVM) Design Pattern, the DataContext (Data Layer) is your application, while UI objects, like Buttons, Labels, DataGrids, and even Windows, are all just user-friendly items that allow a user to easily interact with the DataContext, which is your actual application and is typically comprised of ViewModels and Models.

How is it used

Whenever you do a basic binding in WPF, you are binding to the DataContext.

For example, when you write

<Label Name="myLabel" Content="{Binding Path=Name}" />

you are binding to myLabel.DataContext.Name, and not to myLabel.Name.

Other binding properties, such as ElementName or RelativeSource, can be used to tell the binding to lookup the property in something other than the current DataContext.

An Example

Lets start with a regular Window. Without setting the DataContext, the window still displays but there is no data behind it.

<Window x:Name="MyWindow" ...>   ...</Window>

Now suppose we set the DataContext to an object of type ClassA in the code-behind when this Window initializes:

public partial class MyWindow: Window{    public MyWindow()    {       InitializeComponent();       this.DataContext = new ClassA();    }}

Now the data layer behind that the Window is an object of type ClassA.

If ClassA has a property called Name, I could add a Label to the window and bind it to Name property of the DataContext, and whatever value is stored in ClassA.Name would get displayed.

<Window x:Name="MyWindow" ...>   <Label Content="{Binding Name}" /></Window>

Now, suppose ClassA has a property called ClassB, and both classes have a property called Name. Here is a block of XAML which illustrates how the DataContext works. It also includes an example of how a control would refer to a property not in its own DataContext.

<!-- DataContext set to ClassA in initialization code --><Window x:Name="MyWindow">      <!-- DataContext here is not specified, so it's inherited          from its parent's DataContext, which is ClassA -->    <StackPanel>          <!-- DataContext inherited from parent, which is              ClassA, so this will display ClassA.Name -->        <Label Content="{Binding Name}" />          <!-- DataContext is still ClassA, however we are               setting it to ClassA.ClassB with a binding -->        <StackPanel DataContext="{Binding ClassB}">             <!-- DataContext inherited from parent, which is                  ClassB, so this will display ClassB.Name -->            <Label Content="{Binding Name}" />             <!-- DataContext is still ClassB, but we are                  binding to the Window's DataContext.Name,                  which is ClassA.Name -->            <Label Content="{Binding                        ElementName=MyWindow,                        Path=DataContext.Name}" />         </StackPanel>         <!-- We've left the StackPanel with its DataContext              bound to ClassB, so this Label's DataContext              is ClassA (inherited from parent StackPanel),              and we are binding to ClassA.ClassB.Name -->        <Label Content="{Binding ClassB.Name}" />    </StackPanel></Window>

As you can see, all the basic bindings look for their value in the data layer (DataContext) of the UI object

Summary

So to summarize, WPF applications have two layers: the UI layer and the Data layer. The data layer for an application starts out as null, and can be set using the DataContext property. UI objects without a DataContext set will inherit their data layer from their parent object. Bindings are used to look up values in the data layer, and display them in the UI layer.

When using the MVVM design pattern, the data layer is your application, while the UI layer just provides a user-friendly way to access the Data layer.

What is this “DataContext” you speak of?

I frequently see new WPF users confused about what the DataContext is, and how it is used. Hopefully, this can help clarify what the DataContext is, and how it is used.

What is the DataContext?

In WPF, there are two layers to an application: the UI layer and the Data layer.

The Data layer for an application starts out as null, and you can set it using the DataContext property. All UI objects will inherit their DataContext from their parent unless you specify otherwise.

When using the Model-View-ViewModel (MVVM) Design Pattern, the DataContext (Data Layer) is your application, while UI objects, like Buttons, Labels, DataGrids, and even Windows, are all just user-friendly items that allow a user to easily interact with the DataContext, which is your actual application and is typically comprised of ViewModels and Models.

How is it used

Whenever you do a basic binding in WPF, you are binding to the DataContext.

For example, when you write

<Label Name="myLabel" Content="{Binding Path=Name}" />

you are binding to myLabel.DataContext.Name, and not to myLabel.Name.

Other binding properties, such as ElementName or RelativeSource, can be used to tell the binding to lookup the property in something other than the current DataContext.

An Example

Lets start with a regular Window. Without setting the DataContext, the window still displays but there is no data behind it.

<Window x:Name="MyWindow" ...>   ...</Window>

Now suppose we set the DataContext to an object of type ClassA in the code-behind when this Window initializes:

public partial class MyWindow: Window{    public MyWindow()    {       InitializeComponent();       this.DataContext = new ClassA();    }}

Now the data layer behind that the Window is an object of type ClassA.

If ClassA has a property called Name, I could add a Label to the window and bind it to Name property of the DataContext, and whatever value is stored in ClassA.Name would get displayed.

<Window x:Name="MyWindow" ...>   <Label Content="{Binding Name}" /></Window>

Now, suppose ClassA has a property called ClassB, and both classes have a property called Name. Here is a block of XAML which illustrates how the DataContext works. It also includes an example of how a control would refer to a property not in its own DataContext.

<!-- DataContext set to ClassA in initialization code --><Window x:Name="MyWindow">      <!-- DataContext here is not specified, so it's inherited          from its parent's DataContext, which is ClassA -->    <StackPanel>          <!-- DataContext inherited from parent, which is              ClassA, so this will display ClassA.Name -->        <Label Content="{Binding Name}" />          <!-- DataContext is still ClassA, however we are               setting it to ClassA.ClassB with a binding -->        <StackPanel DataContext="{Binding ClassB}">             <!-- DataContext inherited from parent, which is                  ClassB, so this will display ClassB.Name -->            <Label Content="{Binding Name}" />             <!-- DataContext is still ClassB, but we are                  binding to the Window's DataContext.Name,                  which is ClassA.Name -->            <Label Content="{Binding                        ElementName=MyWindow,                        Path=DataContext.Name}" />         </StackPanel>         <!-- We've left the StackPanel with its DataContext              bound to ClassB, so this Label's DataContext              is ClassA (inherited from parent StackPanel),              and we are binding to ClassA.ClassB.Name -->        <Label Content="{Binding ClassB.Name}" />    </StackPanel></Window>

As you can see, all the basic bindings look for their value in the data layer (DataContext) of the UI object

Summary

So to summarize, WPF applications have two layers: the UI layer and the Data layer. The data layer for an application starts out as null, and can be set using the DataContext property. UI objects without a DataContext set will inherit their data layer from their parent object. Bindings are used to look up values in the data layer, and display them in the UI layer.

When using the MVVM design pattern, the data layer is your application, while the UI layer just provides a user-friendly way to access the Data layer.

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902