This paragraph is translated from the original, Original Address
Different points of data binding Itemsource and DataContext in WPF:
1.DataContext is generally a non-aggregate object, and Itemsource expects the data source to be a collection object.
2.DataContext is a dependency attribute (Dependency property) defined in the FrameworkElement class, ItemsSource is defined in the ItemsControl class. All classes (controls) that inherit from FrameworkElement can use the DataContext property and assign a value to it, but we can only assign ItemsSource to a collection object
3.DataContext cannot produce a template, it can only be used to filter out data for other controls to bind. The main function of ItemsSource is to provide data to the template.
4.DataContext is mainly used to crawl some of the child elements need to use data, to ensure that child elements can use data smoothly. ItemsSource is not used to share data, it is only valid for well-defined elements.
The 4th really does not know how to translate. Finally, attach the original text.
In this post I'll try to illustrate the difference between DataContext and ItemsSource property in SILVERLIGHT/WPF. These the properties don ' t serve the same purpose.
- DataContext expects an object type where ItemsSource expects IEnumerable type objects.
- DataContext is a dependency property was exposed by FrameworkElement base Class,where as ItemsSource are defined by the Item SControl class. All the descendants of FrameworkElement can utilize, the DataContext property and set a object to its value. But we can be only set a type of IEnumerable (or instance of class, derives from).
- DataContext does not generate template, it is only used to the hold common data for other controls to bind. In terms of ItemsSource, it's mainly used to generate template regardless of you set it in XAML or in the code B Ehind.
- DataContext is mainly used-to-hold common data, and child want to share. Thus it can be inherited by other child elements without problem. But for ItemsSource, it isn't used to share data in the visual tree. It's only valid for the element, that defined. There is still one thing to be noted are the child element can override the DataContext of the perent DataContext no M Ater directly or indirectly.
Examples:
Suppose we have the a person Class which has a property Name. Now in Xaml we can say like:
<StackPanelx:name= "Parent"><stackpanel.resources><Local:personx:key= "Person"></stackpanel.resources><ListBoxItemsSource="{Binding source={staticresource person}}"><listbox.itemtemplate><DataTemplate><TextBoxText="{Binding Path=name}"/></DataTemplate></listbox.itemtemplate></ListBox></StackPanel>
If you run this code in the ListBox, you'll get to see values depending on list<person> object. But if we change the ItemsSource to DataContext then you'll be is not able to see anything because DataContext doesn ' t gene Rate templates in any cases. If you set DataContext Still, you should have the-set the ItemsSource property as this:
<DataContext= "{Binding source={staticresource person}}" ItemsSource= "{Binding}">
Conclusion:
In a word, if we had several child elements that would share a common data source, we can set DataContext property for the Parent elements. And we use the ItemsSource for ItemsSource in the most cased to generate template. Like:
<StackPanelDataContext="{Binding person"}><TextBoxText="{Binding FName}"/><TextBoxText="{Binding LName}"/></StackPanel>
ItemsSource and DataContext different points in WPF