Robin's Blog

记录 积累 学习 成长

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  79 随笔 :: 790 文章 :: 74 评论 :: 70万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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. 

posted on   Robin99  阅读(4008)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何调试 malloc 的底层源码
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 因为Apifox不支持离线,我果断选择了Apipost!
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示