DataControlBase.ColumnGeneratorTemplateSelector Property

Gets or sets a column template selector. This is a dependency property.

Namespace: DevExpress.Xpf.Grid

Assembly: DevExpress.Xpf.Grid.v21.1.Core.dll

Declaration

public DataTemplateSelector ColumnGeneratorTemplateSelector { get; set; }

Property Value

Type Description
DataTemplateSelector

A column template selector.

Remarks

You can define columns in a ViewModel and display them in the GridControl. To do this, follow the steps below:

  1. Create a collection of grid columns in a ViewModel and specify a data template that generates columns.

  2. Assign the column collection to the ColumnsSource property and the column template to the ColumnGeneratorTemplate property.

If you have multiple column templates, implement custom logic to choose a template:

  1. Create a template selector – a class that chooses a template based on a condition. This class should be derived from the DataTemplateSelector class.
  2. Override the SelectTemplate method to return a template that meets the condition.
  3. Assign an instance of the template selector class to the ColumnGeneratorTemplateSelector property.
<Window.Resources>
    <!-- ... -->
    <local:ColumnTemplateSelector x:Key="ColumnTemplateSelector"
                                  DefaultColumnTemplate ="{StaticResource DefaultColumnTemplate}"
                                  LookupColumnTemplate ="{StaticResource LookupColumnTemplate}"/>
</Window.Resources>
<dxg:GridControl ... 
                 ColumnsSource="{Binding Columns}"
                 ColumnGeneratorTemplateSelector="{StaticResource ColumnTemplateSelector}"/>
using System.Windows;
using System.Windows.Controls;

namespace ColumnsSample {
    public class ColumnTemplateSelector : DataTemplateSelector {
        public DataTemplate DefaultColumnTemplate { get; set; }
        public DataTemplate LookupColumnTemplate { get; set; }

        public override DataTemplate SelectTemplate(object item, DependencyObject container) {
            Column column = item as Column;
            if(column == null) return null;
            switch(column.Settings) {
                case SettingsType.Default:
                    return DefaultColumnTemplate;
                case SettingsType.Lookup:
                    return LookupColumnTemplate;
            }
            return null;
        }
    }
}

If you specify both the DataControlBase.ColumnGeneratorTemplate and ColumnGeneratorTemplateSelector, the GridControl uses the template the template selector returns.

Refer to the following help topic for more information: How to: Bind the Grid to a Collection of Columns.

See Also