ItemsSource 和 DataContext 之间的区别与 ListBox 有关 [英] Difference between ItemsSource and DataContext as pertains to ListBox

查看:16
本文介绍了ItemsSource 和 DataContext 之间的区别与 ListBox 有关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太了解 ItemsSource 和 DataContext 之间的区别.有人可以解释它并用例子来支持它吗?我什么时候会使用其中一个.

I am not quite grokking the difference between ItemsSource and DataContext. Can someone explain it and back it up with examples? When would I use one or the other.

我正在阅读文档,它说我可以使用 DataContext 进行绑定,但是我向它抛出了一个 ObservableCollection 并且列表中没有显示任何内容.如果我在 ItemsSource 上抛出相同的集合,它就可以正常工作.

I am reading the docs and it says that I can bind using DataContext, but I throw an ObservableCollection at it and nothing shows up in the list. If I throw the same collection at the ItemsSource, it works fine.

推荐答案

控件(包括 ListBox)根本不使用 DataContext 的值.其目的是为数据绑定提供上下文.

Controls (including the ListBox) don't do anything with the value of DataContext at all. Its purpose is to provide a context for data bindings.

假设您有一个 ListBox "myList" 和一个 MyData "myData".MyData 类型具有 ObservableCollection 类型的属性People",而 Person 类型具有字符串属性Forename"和姓氏".

Lets assume you have a ListBox "myList" and a MyData "myData". The MyData type has a property "People" of type ObservableCollection<Person> and in turn the Person type has the string properties "Forename" and "Surname".

以下所有内容都是等价的:-

All of the following are equivalent:-

 myList.ItemsSource = myData.People;

 myList.DataContext = myData;
 myList.SetBinding(ItemsControl.ItemsSourceProperty, new Binding("People"));

 myList.DataContext = myData.People;
 myList.SetBinding(ItemsControl.ItemsSourceProperty, new Binding());

通常,虽然绑定是在 Xaml 中配置的,并且 LayoutRoot 的 DataContext 被分配了数据对象:-

Typically though bindings are configured in Xaml and the DataContext of the LayoutRoot is assigned the data object:-

 LayoutRoot.DataContext = myData;

您可能有以下 Xaml:-

you might have the following Xaml:-

 <Grid x:Name="LayoutRoot">
   <ListBox x:Name="myList" ItemsSource="{Binding People}">
     <ListBox.ItemTemplate>
       <DataTemplate>
         <StackPanel Orientation="Horizontal">
           <TextBlock Text="{Binding Forename}" Margin="2" />
           <TextBlock Text="{Binding Surname}" Margin="2" />
         </StackPanel>
       </DataTemplate>
     </ListBox.ItemTemplate>
   </ListBox>
 </Grid>

您会注意到这里的一些事情.myList"的 DataContext 根本没有分配.在这种情况下,将遍历控件的祖先树,直到找到具有分配给 DataContext 属性的值的祖先.

You'll note a couple of things here. The DataContext of "myList" is not assigned at all. In this case the control's ancestor tree is walked until an ancestor is found that does have a value assigned to the DataContext property.

此外,为每个 Person 实例动态生成的每个 ListBoxItem 都将该 Person 实例分配为其 DataContextForename 和 Surname 绑定是如何工作的.

Also each ListBoxItem dynamically generated for each Person instance has that Person instance assigned as its DataContext which is how the Forename and Surname bindings manage to work.

这篇关于ItemsSource 和 DataContext 之间的区别与 ListBox 有关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆