Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between a WPF DataGrid's Items and ItemsSource properties?

Tags:

c#

wpf

datagrid

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?

like image 859
KSwift87 Avatar asked Apr 25 '17 16:04

KSwift87


People also ask

How to use DataGrid and its itemssource property with list?

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.

What is the difference between DataContext and itemssource in WPF/Silverlight?

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.

What happens when you set itemssource to null in WPF?

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".

What is WPF DataGrid?

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


1 Answers

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.

like image 135
Henk Holterman Avatar answered Nov 07 '22 16:11

Henk Holterman