WPF中 ItemsSource 和DataContext不同點


此段為原文翻譯而來,原文地址

WPF 中 數據綁定 ItemSource和 DataContext的不同點:

1.DataContext 一般是一個非集合性質的對象,而ItemSource 更期望數據源是 集合對象。

2.DataContext 是 FrameworkElement 類中定義的一個依賴屬性(Dependency property),ItemsSource是 在ItemsControl 類中定義的。所有繼承自FrameworkElement 的類(控件)都可以使用DataContext屬性並給其賦值,但我們只能給ItemsSource賦值為集合對象

3.DataContext不能產生模板,它只能用來篩選出數據,供其它控件來綁定。而ItemsSource主要作用就是給模板提供數據。

4.DataContext主要用來抓取一些子元素需要使用的數據,以保證子元素能夠順利的使用數據。ItemsSource不會用來分享數據,它只是對定義好的元素有效。

 

第四點的實在不知道如何翻譯。最后附上原文。

In this post I will try to illustrate the difference between DataContext and ItemsSource property in Silverlight/WPF. These two properties don't serve the same purpose.

  1. DataContext expects an object type where ItemsSource expects IEnumerable type objects.
  2. DataContext is a dependency property is exposed by FrameworkElement base class,where as ItemsSource is defined by the ItemsControl class. All the descendants of FrameworkElement can utilize the DataContext property and set an object to its value. But we can only set a type of IEnumerable(or instance of class that derives from).
  3. DataContext does not generate template, it only used to hold common data for other controls to bind. In terms of ItemsSource property, it is mainly used to generate template regardless of you set it in XAML or in the code behind.
  4. DataContext is mainly used to hold common data that other child want to share. Thus it can be inherited by other child elements without problem. But for ItemsSource, it not used to share data in the visual tree. It is only valid for the element that defined. There is still one thing to be noted is that the child element can override the DataContext of the perent DataContext no mater directly or indirectly.

Examples:
Suppose we have a Person Class which has a property Name. Now in Xaml we can say like:

<StackPanel x:Name="Parent">
<StackPanel.Resources>
<local:Person x:Key="person">
</StackPanel.Resources>
<ListBox ItemsSource="{Binding Source={StaticResource person}}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>

If you run this code in the ListBox you will get to see values depending on List<Person> object. But if we change the ItemsSource to DataContext then you will be not able to see anything because DataContext doesn't generate templates in any cases. If you set datacontext still you have to set the ItemsSource property like this:

<ListBox DataContext="{Binding Source={StaticResource person}}" ItemsSource="{Binding}">

Conclusion:
In a word, if we have several child elements that will share a common data source, we can set DataContext property for the parent elements. And we use ItemsSource for ItemsSource in most cased to generate template. Like:

<StackPanel DataContext="{Binding Person"}>
<TextBox Text="{Binding FName}"/>
<TextBox Text="{Binding LName}"/>
</StackPanel>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM