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" ...
随机推荐
- windows服务器修改登录密码
1. 右键我的电脑---管理 2. 计算机管理---本地用户和组--用户 3. 右键administrator用户,选择修改密码 4. 点击继续进入下一步 5. 输入新密码并保存
- [bzoj3998][TJOI2015]弦论-后缀自动机
Brief Description 给定一个字符串, 您需要求出他的严格k小子串或非严格k小子串. Algorithm Design 考察使用后缀自动机. 首先原串建SAM, 然后如果考察每个状态代表 ...
- 日程管理 FullCalendar
日程管理,采用著名组件FullCalendar日历插件实现FullCalendar提供了丰富的属性设置和方法调用,开发者可以根据FullCalendar提供的API快速完成一个日历日程的开发1.实现按 ...
- linux下简洁优化部署tomcat应用
本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 摘要: 本文是自己根据公司架构部署tomcat方法整理出来的文本 修 ...
- 编码问题 php字符编码转换类
各种平台和软件打开显示的编码问题,需要使用不同的编码,根据我们不同的需求. php 字符编码转换类,支持ANSI.Unicode.Unicode big endian.UTF-8.UTF-8+Bom ...
- java中的mvc和三层结构究竟是什么关系
一件事,要知其然往往很简单,要知其所以然通常不是那么容易,就如最近重新巩固spring的过程中,就觉得还有许多问题其实并不是十分明了. 屈指一算,手头上做过的正式项目也有了四五六七个了,不管用的数据库 ...
- Android HotFix动态加载框架介绍
HotFix(Deprecated) https://github.com/dodola/HotFix 请关注 RocooFix 我重新写了一个RocooFix框架,解决了Nuwa因为Gradle1. ...
- R语言︱LDA主题模型——最优主题数选取(topicmodels)+LDAvis可视化(lda+LDAvis)
每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 笔者寄语:在自己学LDA主题模型时候,发现该模 ...
- 蜻蜓FM 涉嫌诈骗投资人和广告主源代码剖析
本文主要内容,引用自知乎的这篇文章:如何评价蜻蜓 FM 伪造用户活跃度等数据 感谢"左莫"."任正"等热心正义的网友--左莫,任正的最早的回答猜测已经被蜻蜓FM ...
- VxWorks各部分初始化流程
一)configAll.h中定义所有定置系统配置的宏 INCLUDED SOFTWARE FACILITIES:定义了基本组件: EXCLUDED FACILITIES:定义了扩充组件,缺省不包括: ...