Back to Devexpress

How to: Bind the Grid to a Collection of Conditional Formatting Rules

wpf-117884-controls-and-libraries-data-grid-mvvm-enhancements-examples-binding-to-a-collection-of-conditional-formatting-rules.md

latest6.0 KB
Original Source

How to: Bind the Grid to a Collection of Conditional Formatting Rules

  • Nov 29, 2023
  • 6 minutes to read

This topic describes how to define conditional formatting rules in a View Model and apply them to the GridControl.

View Example: How to Bind the GridControl to Conditional Formatting Rules Specified in a ViewModel

Apply Conditional Formatting Rules specified in a ViewModel to the GridControl

This example creates a conditional formatting rule that highlights rows.

Specify Format Conditions in the ViewModel

  1. Create an enumeration that lists comparison rules you want to use in conditional formatting rules:

  2. Create a class that describes a conditional formatting rule:

  3. Specify a collection of conditional formatting rules in the ViewModel:

Create a Format Condition Template

  1. To specify a rule’s comparison operator, set the FormatCondition.ValueRule property to a ConditionRule enumeration value. Create an ObjectToObjectConverter to map your ConditionRule enumeration values to the ConditionRule enumeration values:

  2. Create a template that generates conditional formatting rules.

Apply Format Conditions to the GridControl

  1. Assign the conditional formatting rule collection to the TableView.FormatConditionsSource property.
  2. Set the TableView.FormatConditionGeneratorTemplate property to a template that generates conditional formatting rules.
xaml
<dxg:GridControl ItemsSource="{Binding Orders}">
    <!-- ... -->
    <dxg:GridControl.View>
        <dxg:TableView FormatConditionsSource="{Binding Rules}"
                       FormatConditionGeneratorTemplate="{StaticResource BackgroundFormat}"/>
    </dxg:GridControl.View>
</dxg:GridControl>

Add Multiple Conditional Formatting Rules

If you want to specify extra format conditions, follow the steps below. This example demonstrates how to add format conditions that change a cell’s font and display an image in this cell.

  1. Specify an enumeration that lists format condition types:

  2. Add a property that returns a format condition type to the Rule class:

  3. Add conditional formatting rules to the Rules collection:

  4. Create templates that generate conditional formatting rules:

Add a Template Selector to Choose among Multiple Templates

Your application now contains the BackgroundTemplate , FontTemplate , and IconTemplate. To choose a template based on the format condition type, create a template selector and assign it to the TableView.FormatConditionGeneratorTemplateSelector property:

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
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>

See Also

Conditional Formatting

Bind to Collections Specified in the ViewModel