[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Collapse]WPF Controls
 [Expand]What's Installed
 [Expand].NET Core 3 Support
 [Expand]Common Concepts
 [Expand]MVVM Framework
 [Collapse]Controls and Libraries
  [Collapse]Data Grid
   [Expand]Getting Started
   [Expand]Implementation Details
   [Expand]Grid View Data Layout
   [Expand]Binding to Data
   [Expand]Master-Detail Data Representation
   [Expand]Data Editing and Validation
   [Expand]Filtering and Searching
   [Expand]Data Summaries
   [Expand]Paging and Scrolling
   [Expand]Focus, Navigation, Selection
   [Expand]Conditional Formatting
   [Expand]Appearance Customization
   [Collapse]MVVM Enhancements
     Binding to a Collection of Columns
     Binding to Total and Group Summaries
     Binding to a Collection of Bands
     Binding to a Collection of Conditional Formatting Rules
   [Expand]Printing and Exporting
   [Expand]Performance Improvement
   [Expand]Design-Time Features
   [Expand]Visual Elements
   [Expand]End-User Capabilities
  [Expand]Ribbon, Bars and Menu
  [Expand]Charts Suite
  [Expand]Pivot Grid
  [Expand]Rich Text Editor
  [Expand]Tree List
  [Expand]Gauge Controls
  [Expand]Map Control
  [Expand]Layout Management
  [Expand]Windows Modern UI
  [Expand]Data Editors
  [Expand]Navigation Controls
  [Expand]Spell Checker
  [Expand]Property Grid
  [Expand]PDF Viewer
  [Expand]TreeMap Control
  [Expand]Gantt Control
  [Expand]Diagram Control
  [Expand]Windows and Utility Controls
   Dialogs, Notifications and Panels
  [Expand]Scheduler (legacy)
 [Expand]Scaffolding Wizard
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

Binding to a Collection of Columns

When engineering a WPF application using the Model View ViewModel (MVVM) architectural pattern, you may be required to describe columns in a Model or ViewModel. The grid can be bound to a collection of objects containing column settings, described in a Model or ViewModel, thus minimizing the need for 'code-behind'.

Expanded View Model Implementation

Assume an Employee view model. It includes the following classes.

  • Employee - a data object that contains employee information (e.g. first and last names, job title, etc.).
  • ViewModel - the employee view model.
  • EmployeeData - supplies employee information to be displayed within the grid control.
  • Column - describes a grid column. This class provides properties that correspond to settings common to all types of grid columns.
  • ComboBoxColumn - corresponds to a grid column with the ComboBoxEdit in-place editor. This class provides the Source property, which contains the list of a combo box item (in this example, these are cities).
  • SettingsType - enumerates possible types of in-place editors used to edit cell values.

If the Columns collection might be changed after it has been assigned to the grid control, it should implement INotifyCollectionChanged, so that changes made within a View Model are automatically reflected by the grid.

Expanded Column Templates and Selector

The Grid Control generates columns based on column templates. Create multiple templates, one template for each column type. Using a single template you can create an unlimited number of columns in unlimited number of grid controls. In this example, there are two column templates: DefaultColumnTemplate and ComboColumnTemplate.

To avoid performance issues when binding to column properties, use the dxci:DependencyObjectExtensions.DataContext attached property. See the example below.

To choose the required template based on the column's type, use the Template Selector. In this example, the template selector is represented by the ColumnTemplateSelector class.


If all grid columns can be described using a single template, you have no need to create a column template selector. Instead, assign this template to the grid's DataControlBase.ColumnGeneratorTemplate property.


You can create a style to specify settings common to all columns generated using different templates. You can specify bindings to ViewModel properties within a style (see FieldName below):

This style should be assigned to the DataControlBase.ColumnGeneratorStyle property.

Expanded Customizing the WPF DXGrid

Finally, specify the grid's DataControlBase.ItemsSource, DataControlBase.ColumnsSource and DataControlBase.ColumnGeneratorTemplateSelector. The DataControlBase.ItemsSource property specifies the grid's data source. The DataControlBase.ColumnsSource property specifies the source from which the grid generates columns. The DataControlBase.ColumnGeneratorTemplateSelector property specifies the column template selector, which returns a template for each column based on its type.

The image below shows the result.

Expanded Examples

This example shows how to put columns and data summary definition logic in the ViewModel and setup the Grid Control.

Expanded See Also

Is this topic helpful?​​​​​​​