ItemSource与DataContext的区别
来源:互联网 发布:网络教学平台官网 编辑:程序博客网 时间:2024/05/17 23:50
DataContext用于绑定单行,ItemSource用于多行绑定。
Those two properties do not serve the same purpose.DataContext is a general (dependency) property of all descendants of FrameworkElement. Is is inherited through the logical tree from parent to children and can be used as an implicit source for DataBinding. It does not do anything by itself, you must basically databind to it.
ItemsSource is the property identifying the source for templated generation of items in an ItemsControl derived control (or HierarchicalDataTemplate).
When you set the value of ItemsSource (either through DataBinding or by code), the control will generate the templated items internally.
Setting DataContext on an ItemsControl will NOT achieve this.
Usually, DataContext is set explicitly at the top of the hierarchy, and sometimes overriden (re-bound explicitly) lower in the hierarchy.
Most of the time, ItemsSource will be bound to a member of DataContext (either direct or indirect, as a sub-property of a property of the DataContext).
To use the canonical example of an Order with a few properties, one of them a collection of OrderLine objects (oh how I hate that example...), You could have a GUI with a first part detailing the information pertaining to the order.
You Window or UserControl DataContext would be data-bound to the Order Object, with a fex TextBlocks/TextBoxes databound to the primary properties of the object.
Somewhere down the line, you would have an itemsControl, (or ListBox, Listview, whatever). Its DataContext would be the Order object because it is inherited. Its ItemsSource property however would need to be data-bound to the "OrderLines" property to display each OrderLine as an item in the list.
For example, assuming a class looking like that (pseudo VB style, sorry)
You could have some XAML looking like that, assuming you set the DataContext on the top level object (a window or a UserControl for example):
Disclaimer; I just typed that in the browser from memory, and I am using Silverlight and not WPF these days, so there might be some errors in there, but the basic idea is here.
So to come back to your original question, using DataContext is not enough to bind an ItemsControl to a list. You need to bind ItemsSource to achieve auto-generation of items in the list.
0 0
- ItemSource与DataContext的区别
- WPF数据绑定——Datacontext和itemsource的区别
- WPF学习笔记——DataContext 与 ItemSource
- WPF学习笔记——DataContext 与 ItemSource
- WPF ItemSource绑定的问题
- silverlight中ItemsSource和DataContext的用法区别
- Linq DataContext与实体
- DataContext的初始化
- DataContext的日志功能
- 强类型的datacontext
- DataContext.ExecuteQuery的问题
- DataContext
- Linq 之DataContext与实体
- wp7中关于ListPicker的ItemSource的一个疑问
- WPF的Combobox的ItemSource第二次绑定(更新)问题
- Silverlight ComboBox的SelectedValue不在ItemSource中的绑定失败问题。
- Silverlight中AutoCompleteBox异步更新自定义的ItemSource
- 【WPF】CommandParameter无法多传参?传递控件自身的ItemSource数据
- union: 一种节省空间的类
- MySQL 查看数据表结构以及详情的方法
- 基于Struts的文件下载
- shell编程小实例
- iOS CocoaPods安装 导入 使用
- ItemSource与DataContext的区别
- iOS10 推送必看(基础篇)
- poj 1011 sticks(木棒) (dfs+剪枝)
- Linux 挂在windows 目录小技巧
- Java WebService 简单实例
- iOS - *** Terminating app due to uncaught exception 'NSRangeException&am
- 冒泡排序,简单插入排序,快速排序,二分查找的JAVA实现
- Dialog总结
- Android guide学习系列(-)——Device Compatibility(设备兼容性)