ItemSource与DataContext的区别

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. 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值