Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]Welcome to DevExpress .NET Documentation
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Collapse]WPF Controls
  Prerequisites
 [Expand]What's Installed
 [Expand]Common Concepts
 [Expand]MVVM Framework
 [Collapse]Controls and Libraries
  [Expand]Windows and Utility Controls
   Dialogs, Notifications and Panels
   Reporting
  [Collapse]Data Grid
   [Expand]Getting Started
   [Expand]Implementation Details
   [Expand]Views
   [Expand]Grid View Data Layout
   [Expand]Binding to Data
   [Expand]Master-Detail Data Representation
   [Expand]Data Editing and Validation
   [Expand]Grouping
   [Expand]Selection
   [Expand]Filtering and Searching
   [Expand]Sorting
   [Expand]Data Summaries
   [Expand]Data Scrolling
   [Expand]Focus and Navigation
   [Expand]Conditional Formatting
   [Expand]Drag-and-Drop
   [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]End-User Interaction
   [Expand]Miscellaneous
   [Expand]Performance Improvement
   [Expand]Design-Time Features
   [Expand]Visual Elements
   [Expand]End-User Capabilities
   [Expand]Examples
  [Expand]Ribbon, Bars and Menu
  [Expand]Charts Suite
  [Expand]Pivot Grid
  [Expand]Scheduler
  [Expand]Spreadsheet
  [Expand]Rich Text Editor
  [Expand]Tree List
  [Expand]Gauge Controls
  [Expand]Map Control
  [Expand]Layout Management
  [Expand]Windows Modern UI
  [Expand]Printing-Exporting
  [Expand]Data Editors
  [Expand]Navigation Controls
  [Expand]Spell Checker
  [Expand]Property Grid
  [Expand]PDF Viewer
  [Expand]TreeMap Control
  [Expand]Diagram Control
  [Expand]Scheduler (legacy)
 [Expand]Scaffolding Wizard
 [Expand]Localization
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Reporting
[Expand]Report Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

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

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.

Note

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.

Note

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

Show Me

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=T273154.

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?​​​​​​​