Skip to main content

DataControlBase.ColumnGeneratorTemplateSelector Property

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

Namespace: DevExpress.Xpf.Grid

Assembly: DevExpress.Xpf.Grid.v24.2.Core.dll

NuGet Package: DevExpress.Wpf.Grid.Core

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.

The following code snippets (auto-collected from DevExpress Examples) contain references to the ColumnGeneratorTemplateSelector property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also