WPF中 ItemsSource 和DataContext不同点
此段为原文翻译而来,原文地址
WPF 中 数据绑定 ItemSource和 DataContext的不同点:
1.DataContext 一般是一个非集合性质的对象,而ItemSource 更期望数据源是 集合对象。
2.DataContext 是 FrameworkElement 类中定义的一个依赖属性(Dependency property),ItemsSource是 在ItemsControl 类中定义的。所有继承自FrameworkElement 的类(控件)都可以使用DataContext属性并给其赋值,但我们只能给ItemsSource赋值为集合对象
3.DataContext不能产生模板,它只能用来筛选出数据,供其它控件来绑定。而ItemsSource主要作用就是给模板提供数据。
4.DataContext主要用来抓取一些子元素需要使用的数据,以保证子元素能够顺利的使用数据。ItemsSource不会用来分享数据,它只是对定义好的元素有效。
第四点的实在不知道如何翻译。最后附上原文。
In this post I will try to illustrate the difference between DataContext and ItemsSource property in Silverlight/WPF. These two properties don't serve the same purpose.
- DataContext expects an object type where ItemsSource expects IEnumerable type objects.
 - DataContext is a dependency property is exposed by FrameworkElement base class,where as ItemsSource is defined by the ItemsControl class. All the descendants of FrameworkElement can utilize the DataContext property and set an object to its value. But we can only set a type of IEnumerable(or instance of class that derives from).
 - DataContext does not generate template, it only used to hold common data for other controls to bind. In terms of ItemsSource property, it is mainly used to generate template regardless of you set it in XAML or in the code behind.
 - DataContext is mainly used to hold common data that other child want to share. Thus it can be inherited by other child elements without problem. But for ItemsSource, it not used to share data in the visual tree. It is only valid for the element that defined. There is still one thing to be noted is that the child element can override the DataContext of the perent DataContext no mater directly or indirectly.
 
Examples:
Suppose we have a Person Class which has a property Name. Now in Xaml we can say like:
<StackPanel x:Name="Parent">
<StackPanel.Resources>
<local:Person x:Key="person">
</StackPanel.Resources>
<ListBox ItemsSource="{Binding Source={StaticResource person}}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
If you run this code in the ListBox you will get to see values depending on List<Person> object. But if we change the ItemsSource to DataContext then you will be not able to see anything because DataContext doesn't generate templates in any cases. If you set datacontext still you have to set the ItemsSource property like this:
<ListBox DataContext="{Binding Source={StaticResource person}}" ItemsSource="{Binding}">
Conclusion:
In a word, if we have several child elements that will share a common data source, we can set DataContext property for the parent elements. And we use ItemsSource for ItemsSource in most cased to generate template. Like:
<StackPanel DataContext="{Binding Person"}>
<TextBox Text="{Binding FName}"/>
<TextBox Text="{Binding LName}"/>
</StackPanel>
WPF中 ItemsSource 和DataContext不同点的更多相关文章
- 关于WPF中ItemsControl系列控件中Item不能继承父级的DataContext的解决办法
		
WPF中所有的集合类控件,子项都不能继承父级的DataContext,需要手动将绑定的数据源指向到父级控件才可以. <DataGridTemplateColumn Header="操作 ...
 - wpf中数据绑定(Datacontext)的应用
		
在winform开发中,我们常用到ado.net进行数据绑定,在编程技术日新月异的今天,这种繁杂的数据绑定方式已不能再适合开发人员,于是微软推出了wpf,更炫的界面美化,更简洁地编写控件,在wpf中使 ...
 - MVVM模式和在WPF中的实现(二)数据绑定
		
MVVM模式解析和在WPF中的实现(二) 数据绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...
 - WPF入门教程系列十八——WPF中的数据绑定(四)
		
六.排序 如果想以特定的方式对数据进行排序,可以绑定到 CollectionViewSource,而不是直接绑定到 ObjectDataProvider.CollectionViewSource 则会 ...
 - WPF中的数据绑定!!!
		
引用自:https://msdn.microsoft.com/zh-cn/magazine/cc163299.aspx 数据点: WPF 中的数据绑定 数据点 WPF 中的数据绑定 John Pap ...
 - WPF中实现自定义虚拟容器(实现VirtualizingPanel)
		
WPF中实现自定义虚拟容器(实现VirtualizingPanel) 在WPF应用程序开发过程中,大数据量的数据展现通常都要考虑性能问题.有下面一种常见的情况:原始数据源数据量很大,但是某一时刻数据容 ...
 - WPF中触发器Trigger、MultiTrigger、DataTrigger、MultiDataTrigger、EventTrigger几种
		
WPF中有种叫做触发器的东西(记住不是数据库的trigger哦).它的主要作用是根据trigger的不同条件来自动更改外观属性,或者执行动画等操作. WPFtrigger的主要类型有:Trigger. ...
 - WPF中TreeView控件的使用案例
		
WPF总体来说还是比较方便的,其中变化最大的主要是Listview和Treeview控件,而且TreeView似乎在WPF是一个备受指责的控件,很多人说他不好用.我这个demo主要是在wpf中使用Tr ...
 - WPF中如何为ItemsControl添加ScrollViewer并显示ScrollBar
		
今天在开发的过程中突然碰到了一个问题,本来的意图是想当ItemsControl中加载的Item达到一定数量时,会出现ScrollViewer并出现垂直的滚动条,但是实际上并不能够达成目标,对于熟手来说 ...
 
随机推荐
- CountDownLatch 多线程,等待所有线程结束
			
CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 主要方法 public CountDownLatch(int count); 构造 ...
 - java中面向对象的三大特性小结
			
java中面向对象的三大特性:封装.继承.多态 封装 把抽象的数据和对数据的操作封装在一起,隐藏变量的实现细节.数据被保护在内部,程序的其他部分只有通过被授权的操作(成员方法)才能对数据进行访问. 1 ...
 - (二)HTML中的部分标签
			
HTML作为一种超文本标记语言,其中用到了大量的标签,昨天主要看了HTML中的图像标签和表格标签. (一)图像标签 <img> <img src="url"/&g ...
 - Trim a Binary Search Tree
			
Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...
 - Maven学习篇一:依赖了解
			
1.依赖配置 <project> ... <dependencies> <dependency> <groupId>xx</groupId> ...
 - Latest SoC
			
From: http://laoyaoba.com/ss6/html/68/n-505768.html 2014年国产ARM SoC芯片巡礼(上) 关注“集微网”,微信点播新闻.随要随有 来源: &l ...
 - 01.IDEA常用快捷键
			
[1.查找] 当前窗口基本查找 ----------- Ctrl + F 返回上次浏览的位置 ----------- Ctrl + Alt + Left 查找类 ----------- Ctrl + ...
 - 19_AOP概述
			
[AOP的使用场景] 性能测试 访问控制 事务管理 日志记录 [AOP相关术语] [ 连接点 Joinpoint ] 程序执行的某个特定位置.(假如Car类有drive()方法,那么在drive()方 ...
 - Vbox安装CentOS7及网络配置
			
安装CentOS7及网络配置 Vbox和其他虚拟机一样,安装完成一个虚拟机,需要配置网络才能实现物理主机和虚拟机之间的访问.虚拟主机和Internet(外网)的访问 1.设置Vbox全局网络 单击主界 ...
 - Oracle:environment variable "PATH" does not exceed the recommended length
			
今天重新安装oracle11g,突然在检测时报了以下错误: Environment variable: "PATH" - This test checks whether the ...