From what I can gather from Intellisense, the difference is the return types and the ItemsSource has a setter whereas Items simply has a getter. Practically speaking though, I do not understand the need for these two properties being separate from each other instead of just being one property.
Could someone explain to me why these are separate properties instead of just one property?
And also, if I'm missing something, could someone please explain to me when I'd want to use one over the other (besides the obvious need of a setter)? E.g., when specifically would I want to use Items over ItemsSource?
This example uses DataGrid and its ItemsSource property with a List. First, create a WPF project and drag a DataGrid to your window. In the XAML, please add the attribute "Loaded" to the "DataGrid" element. Then: Visual Studio will create the DataGrid_Loaded event handler.
Difference between DataContext and ItemsSource in WPF/Silverlight. In terms of ItemsSource property, it is mainly used to generate template regardless of you set it in XAML or in the code behind. 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.
When ItemsSource is set, the Items property cannot be used to control the displayed values. If you later set ItemsSource to null, Items becomes usable again. To demonstrate data binding with ItemsSource, we need a sample project. Create a new WPF application in Visual Studio, naming the solution, "ItemsSourceDemo".
WPF - Datagrid - A DataGrid is a control that displays data in a customizable grid. It provides a flexible way to display a collection of data in rows and columns. The hierarchi
What's the difference between a WPF DataGrid's Items and ItemsSource properties?
A DataGrid is an ItemsControl so this applies to all other ItemsControl classes as well.
The Items property is an ItemCollection and is filled in through XAML. It holds objects but is intended for FrameworkElements.
The ItemsSource is bindable to a simple IEnumerable, with the ability to support INotifyCollectionChanged when available. It also supports DataTemplates.
when specifically would I want to use Items over ItemsSource?
ItemsSource is for databinding to a ViewModel.
You would use Items only in a few situations where you have a fixed number of XAML items. Unlikely for a Grid, more usable for a ComboBox.
You never use both at the same time.
This MSDN Page shows the typical usage for both.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With