WPF之DataContext(转)
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.
WPF之DataContext(转)的更多相关文章
- WPF之DataContext
1. 继承属性: DataContext is a property on FrameworkElement (base class for all WPF Controls) and is impl ...
- [WPF] How to bind to data when the datacontext is not inherited
原文:[WPF] How to bind to data when the datacontext is not inherited 原文地址:http://www.thomaslevesque.co ...
- WPF入门(1)——DataContext
在WPF中,应用程序有两层:UI层和Data层.这里新建一个项目说明哪些是UI层,哪些是数据层. UI层很明显,就是用户看到的界面.但是数据层并不是下图所示: 上图中是UI层view的后台代码.当然, ...
- 【我们一起写框架】MVVM的WPF框架(二)—绑定
MVVM的特点之一是实现数据同步,即,前台页面修改了数据,后台的数据会同步更新. 上一篇我们已经一起编写了框架的基础结构,并且实现了ViewModel反向控制Xaml窗体. 那么现在就要开始实现数据同 ...
- [No000012E]WPF(6/7):概念绑定
WPF 的体系结构,标记扩展,依赖属性,逻辑树/可视化树,布局,转换等.今天,我们将讨论 WPF 最重要的一部分——绑定.WPF 带来了优秀的数据绑定方式,可以让我们绑定数据对象,这样每次对象发生更改 ...
- 关于WPF中ItemsControl系列控件中Item不能继承父级的DataContext的解决办法
WPF中所有的集合类控件,子项都不能继承父级的DataContext,需要手动将绑定的数据源指向到父级控件才可以. <DataGridTemplateColumn Header="操作 ...
- WPF设置Window的数据上下文(DataContext)为自身
WPF设置Window的数据上下文(DataContext)为自身的XAML: DataContext="{Binding RelativeSource={RelativeSource Se ...
- WPF中 ItemsSource 和DataContext不同点
此段为原文翻译而来,原文地址 WPF 中 数据绑定 ItemSource和 DataContext的不同点: 1.DataContext 一般是一个非集合性质的对象,而ItemSource 更期望数据 ...
- WPF学习笔记——DataContext 与 ItemSource
作为一个WPF新手,在ListBox控件里,我分不清 DataContext 与 ItemSource的区别. 在实践中,似乎: <ListBox x:Name="Lst" ...
随机推荐
- 洛谷P1171 售货员的难题【状压DP】
题目描述 某乡有n个村庄(1 输入格式: 村庄数n和各村之间的路程(均是整数). 输出格式: 最短的路程. 输入样例: 3 0 2 1 1 0 2 2 1 0 输出样例 3 说明 输入解释 3 {村庄 ...
- DML、DDL、DCL的区别
DML(data manipulation language): 它们是SELECT.UPDATE.INSERT.DELETE,就象它的名字一样,这4条命令是用来对数据库里的数据进行操作的语言DDL( ...
- 【Unity3D技术文档翻译】第1.3篇 创建 AssetBundles
上一章:[Unity3D技术文档翻译]第1.2篇 为打包 AssetBundles 准备资产 本章原文所在章节:[Unity Manual]→[Working in Unity]→[Advanced ...
- 排序算法(Java实现)
这几天一直在看严蔚敏老师的那本<数据结构>那本书.之前第一次学懵懵逼逼,当再次看的时候,发觉写的是非常详细,非常的好. 那就把相关的排序算法用我熟悉的Java语言记录下来了.以下排序算法是 ...
- C/C++语言简介之程序结构
C语言的模块化程序结构用函数来实现,即将复杂的C程序分为若干模块,每个模块都编写成一个C函数,然后通过主函数调用函数及函数调用函数来实现一大型问题的C程序编写,因此常说:C程序=主函数+子函数.因此, ...
- Sping Boot入门到实战之入门篇(一):Spring Boot简介
该篇为Spring Boot入门到实战系列入门篇的第一篇.对Spring Boot做一个大致的介绍. 传统的基于Spring的Java Web应用,需要配置web.xml, applicationCo ...
- Windows下使用Sublime text3快速编辑Linux文件,写Shell
所需要配合的工具是WinSCP 添加完毕之后直接在目录下双击要编辑的shell脚本文件,即可弹出Sublime Text的编辑器 然后咱通过Putty看看Linux虚拟机上的文件有没有发生变化
- 关于微信分享到朋友圈(Thinkphp框架下实现)
PHP部分 扩展类代码部分: <?php namespace Think; class JsSdk { private $appId; private $appSecre ...
- PHP如何防止XSS攻击
PHP防止XSS跨站脚本攻击的方法:是针对非法的HTML代码包括单双引号等,使用htmlspecialchars()函数 . 在使用htmlspecialchars()函数的时候注意第二个参数, 直接 ...
- 剑指offer 丑数
思路:可以发现,每个丑数都是由以前的丑数得到.当前丑数一定是之前丑数能够得到的最小丑数. AC代码 class Solution { public: int GetUglyNumber_Solutio ...