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)

Public Class Order   Property OrderDate as Date    Property Customer as Customer    Property OrderLines as List(of OrderLine)  End Class  Public Class Customer   Property Name as String  End Class  Public Class OrderLine   Property Product as Product   Property Quantity as Integer End Class   Public Class Product   Property Name as String End Class 


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):

<StackPanel>   <StackPanel Orientation="Horizontal"    <TextBLock Text="Customer:" />     <TextBLock Text="{Binding Customer.Name}" />    </StackPanel>   <StackPanel Orientation="Horizontal"    <TextBLock Text="Date:" />     <TextBLock Text="{Binding Date}" />    </StackPanel>  <ListBox ItemsSource="{Binding OrderLines}"  <ListBox.ItemTemplate>     <DataTemplate TargetType="{x:Type local:OrderLine}"      <StackPanel Orientation="Horizontal"        <TextBlock Text="{Binding Product.Name}" />         <TextBlock Text=" Qty= " />         <TextBlock Text="{Binding Quantity}" />       </StackPanel>      </DataTemplate>   </ListBox.ItemTemplate> </ListBox> </StackPanel> 

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