I am sharing the code of an Attached Behavior that dynamically populates the columns of an Infragistics data grid; XamDataGrid Control. This post is based on XamDataGrid–Dynamically Create and Data Bind Columns with an Editor of your Choice.

 

So What’s New Here?

  1. The dynamic column creation implementation is packaged up in an extensible and unit testable pattern using WPF attached behavior
  2. I am not relying on the FieldLayoutInitialized event in the attached behavior, instead on PropertyChangedCallback (justification below)
  3. Replaces deprecated Infragistics API.
  4. Built with .NET 4.6, Prism 6.2, and Infragistics WPF v16.1 (trial version)

Deliverable

The complete implementation is packaged up in a downloadable demo app as shown in the screenshot below. For fun reasons different rows can have different number of columns.

Demo App - XamDataGrid Dynamic Columns using Attached Behavior
Demo App – XamDataGrid Dynamic Columns using Attached Behavior

View

View Models

Behaviour

Providers

Gets called when the button is clicked.

What about FieldLayoutInitialized Event

Initially i tried to hook to the FieldLayoutInitialized event of an AssociateObject in the attached behavior class, however i hit some limitations:

  1. The initialisation of FieldLayout which fires the event took place earlier than populating the Columns custom dependency property change event. Hence, this.Columns value of in the event handler was null.
  2. Flexibility to regenerate columns based on ‘Max. Number of Columns’ user input. Whenever the property (OrderViewModels) that is bound to Columns dependency property is updated it calls the value changed call back which in turn will render the grid field layout. This is achieved by telling it so during DependencyProperty.Register; PropertyMetadata(PropertyChangedCallback).

Source Code

Download.

 

Leave a Reply

Your email address will not be published. Required fields are marked *