Back to Devexpress

DataControlBase.ColumnGeneratorTemplateSelector Property

wpf-devexpress-dot-xpf-dot-grid-dot-datacontrolbase-35bc7d4a.md

latest6.3 KB
Original Source

DataControlBase.ColumnGeneratorTemplateSelector Property

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

Namespace : DevExpress.Xpf.Grid

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

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
public DataTemplateSelector ColumnGeneratorTemplateSelector { get; set; }
vb
Public Property ColumnGeneratorTemplateSelector As DataTemplateSelector

Property Value

TypeDescription
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.
xaml
<Window.Resources>
    <!-- ... -->
    <local:ColumnTemplateSelector x:Key="ColumnTemplateSelector"
                                  DefaultColumnTemplate ="{StaticResource DefaultColumnTemplate}"
                                  LookupColumnTemplate ="{StaticResource LookupColumnTemplate}"/>
</Window.Resources>
<dxg:GridControl ... 
                 ColumnsSource="{Binding Columns}"
                 ColumnGeneratorTemplateSelector="{StaticResource ColumnTemplateSelector}"/>
csharp
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;
        }
    }
}
vb
Imports System.Windows
Imports System.Windows.Controls

Namespace ColumnsSample
    Public Class ColumnTemplateSelector
        Inherits DataTemplateSelector

        Public Property DefaultColumnTemplate() As DataTemplate
        Public Property LookupColumnTemplate() As DataTemplate

        Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As DependencyObject) As DataTemplate
            Dim column As Column = TryCast(item, Column)
            If column Is Nothing Then
                Return Nothing
            End If
            Select Case column.Settings
                Case SettingsType.Default
                    Return DefaultColumnTemplate
                Case SettingsType.Lookup
                    Return LookupColumnTemplate
            End Select
            Return Nothing
        End Function
    End Class
End Namespace

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.

wpf-data-grid-bind-columns-to-viewmodel-collection/CS/ColumnsSample/MainWindow.xaml#L36

xml
<dxg:GridControl ItemsSource="{Binding Source}" ColumnsSource="{Binding Columns}"
                 ColumnGeneratorTemplateSelector="{StaticResource ColumnTemplateSelector}">
    <dxg:GridControl.View>

wpf-bind-gridcontrol-to-dynamic-data/CS/Unbound_Columns/MainWindow.xaml#L35

xml
<dxg:GridControl
    ColumnGeneratorTemplateSelector="{StaticResource selector}"
    ColumnsSource="{Binding Columns}"

See Also

DataControlBase Class

DataControlBase Members

DevExpress.Xpf.Grid Namespace