Back to Devexpress

TableView.FormatConditionGeneratorTemplateSelector Property

wpf-devexpress-dot-xpf-dot-grid-dot-tableview-fa909c7d.md

latest5.5 KB
Original Source

TableView.FormatConditionGeneratorTemplateSelector Property

Gets or sets the format condition template selector. This is a dependency property.

Namespace : DevExpress.Xpf.Grid

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

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

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

Property Value

TypeDescription
DataTemplateSelector

A format condition template selector.

|

Remarks

You can define conditional formatting rules in a ViewModel and apply them to the GridControl. To do this, follow the steps below:

  1. Create a collection of conditional formatting rules in a ViewModel and specify a data template that generates format conditions.

  2. Assign the conditional formatting rule collection to the TableView.FormatConditionsSource property and the format condition template to the TableView.FormatConditionGeneratorTemplate property:

If you have multiple format condition 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 FormatConditionGeneratorTemplateSelector property.
xaml
<Window.Resources>
    <!-- ... -->
    <local:FormatConditionSelector x:Key="FormatConditionSelector" 
                                   BackgroundTemplate="{StaticResource BackgroundTemplate}" 
                                   FontTemplate="{StaticResource FontTemplate}" 
                                   IconTemplate="{StaticResource IconTemplate}"/>
</Window.Resources>
<dxg:GridControl ItemsSource="{Binding Orders}">
    <!-- ... -->
    <dxg:GridControl.View>
        <dxg:TableView FormatConditionsSource="{Binding Rules}"
                       FormatConditionGeneratorTemplateSelector="{StaticResource FormatConditionSelector}"/>
    </dxg:GridControl.View>
</dxg:GridControl>
csharp
public class FormatConditionSelector : DataTemplateSelector {

    public DataTemplate BackgroundTemplate { get; set; }
    public DataTemplate FontTemplate { get; set; }
    public DataTemplate IconTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container) {
        FormattingRule rule = item as FormattingRule;
        if(rule == null) return null;
        switch(rule.Type) {
            case FormattingType.Font:
                return FontTemplate;
            case FormattingType.Background:
                return BackgroundTemplate;
            case FormattingType.Icon:
                return IconTemplate;
        }
        return null;
    }
}
vb
Public Class FormatConditionSelector
    Inherits DataTemplateSelector

    Public Property BackgroundTemplate As DataTemplate
    Public Property FontTemplate As DataTemplate
    Public Property IconTemplate As DataTemplate

    Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As DependencyObject) As DataTemplate
        Dim rule As FormattingRule = TryCast(item, FormattingRule)
        If rule Is Nothing Then Return Nothing

        Select Case rule.Type
            Case FormattingType.Font
                Return FontTemplate
            Case FormattingType.Background
                Return BackgroundTemplate
            Case FormattingType.Icon
                Return IconTemplate
        End Select

        Return Nothing
    End Function
End Class

If you specify both the TableView.FormatConditionGeneratorTemplate and FormatConditionGeneratorTemplateSelector properties, 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 Conditional Formatting Rules.

See Also

FormatConditionsSource

TableView Class

TableView Members

DevExpress.Xpf.Grid Namespace