此段为原文翻译而来,原文地址

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.

  1. DataContext expects an object type where ItemsSource expects IEnumerable type objects.
  2. 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).
  3. 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.
  4. 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不同点的更多相关文章

  1. 关于WPF中ItemsControl系列控件中Item不能继承父级的DataContext的解决办法

    WPF中所有的集合类控件,子项都不能继承父级的DataContext,需要手动将绑定的数据源指向到父级控件才可以. <DataGridTemplateColumn Header="操作 ...

  2. wpf中数据绑定(Datacontext)的应用

    在winform开发中,我们常用到ado.net进行数据绑定,在编程技术日新月异的今天,这种繁杂的数据绑定方式已不能再适合开发人员,于是微软推出了wpf,更炫的界面美化,更简洁地编写控件,在wpf中使 ...

  3. MVVM模式和在WPF中的实现(二)数据绑定

    MVVM模式解析和在WPF中的实现(二) 数据绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...

  4. WPF入门教程系列十八——WPF中的数据绑定(四)

    六.排序 如果想以特定的方式对数据进行排序,可以绑定到 CollectionViewSource,而不是直接绑定到 ObjectDataProvider.CollectionViewSource 则会 ...

  5. WPF中的数据绑定!!!

    引用自:https://msdn.microsoft.com/zh-cn/magazine/cc163299.aspx  数据点: WPF 中的数据绑定 数据点 WPF 中的数据绑定 John Pap ...

  6. WPF中实现自定义虚拟容器(实现VirtualizingPanel)

    WPF中实现自定义虚拟容器(实现VirtualizingPanel) 在WPF应用程序开发过程中,大数据量的数据展现通常都要考虑性能问题.有下面一种常见的情况:原始数据源数据量很大,但是某一时刻数据容 ...

  7. WPF中触发器Trigger、MultiTrigger、DataTrigger、MultiDataTrigger、EventTrigger几种

    WPF中有种叫做触发器的东西(记住不是数据库的trigger哦).它的主要作用是根据trigger的不同条件来自动更改外观属性,或者执行动画等操作. WPFtrigger的主要类型有:Trigger. ...

  8. WPF中TreeView控件的使用案例

    WPF总体来说还是比较方便的,其中变化最大的主要是Listview和Treeview控件,而且TreeView似乎在WPF是一个备受指责的控件,很多人说他不好用.我这个demo主要是在wpf中使用Tr ...

  9. WPF中如何为ItemsControl添加ScrollViewer并显示ScrollBar

    今天在开发的过程中突然碰到了一个问题,本来的意图是想当ItemsControl中加载的Item达到一定数量时,会出现ScrollViewer并出现垂直的滚动条,但是实际上并不能够达成目标,对于熟手来说 ...

随机推荐

  1. python网络编程——简单例子

    客户端(client.py) import socket import sys port = 70 host = sys.argv[1] filename = sys.argv[2] s = sock ...

  2. WPF 父子窗体联动

    问题: 近段时间,由于项目上的一些原因,设计到在WPF项目使用引用COM组件的问题,部分WPF元素浮动在COM组件之上,并且实现拖.停靠.放大等功能(子窗体不要求等比缩放,只要位置跟随主窗体即可),如 ...

  3. nyoj 546——Divideing Jewels——————【dp、多重背包板子题】

    Divideing Jewels 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Mary and Rose own a collection of jewells. ...

  4. Python 函数运行时更新

    Python 动态修改(运行时更新) 特性 实现函数运行时动态修改(开发的时候,非线上) 支持协程(tornado等) 兼容 python2, python3 安装 pip install realt ...

  5. bzoj 3576: [Hnoi2014]江南乐

    Description 小A是一个名副其实的狂热的回合制游戏玩家.在获得了许多回合制游戏的世界级奖项之后,小A有一天突然想起了他小时候在江南玩过的一个回合制游戏. 游戏的规则是这样的,首先给定一个数F ...

  6. ajax实现的点击数目加1代码实例

    ajax实现的点击数目加1代码实例:在点击按钮实现数字增加效果代码实例一章节中,介绍如何点击按钮实现数字加1的效果,但是好像并没有什么实际用处,下面就分享一段相对完整的能够在实际应用中派上用场的代码, ...

  7. 深入理解JavaScript系列(7):S.O.L.I.D五大原则之开闭原则OCP

    前言 本章我们要讲解的是S.O.L.I.D五大原则JavaScript语言实现的第2篇,开闭原则OCP(The Open/Closed Principle ). 开闭原则的描述是: Software ...

  8. asp 2.0 ajax triggers 触发更新

  9. /Date(1410019200000+0800)/如何转换为date对象

    <script type="text/javascript">var s = '/Date(1410019200000+0800)/ '; s.replace(/Dat ...

  10. 如何更新maven需要的jar包

    第一次使用maven,检出项目生成时出现缺少xxx.jar,目录在C盘下: 拿mybatis-spring-1.2.2.jar来说,发现在C:\Users\Administrator\.m2\repo ...