Back to Devexpress

Formatting Values Using Icon Sets

wpf-118922-controls-and-libraries-data-grid-conditional-formatting-conditional-formats-formatting-values-using-icon-sets.md

latest6.9 KB
Original Source

Formatting Values Using Icon Sets

  • Jan 27, 2023
  • 4 minutes to read

The Icon Sets conditional format allows you to use predefined or custom icon sets to format different value ranges.

The image below shows a grid column whose Profit cells contain icons corresponding to numeric values.

Add Rules in Code

Create the IconSetFormatCondition class instance and specify the following settings to create a conditional format in code:

The following code sample illustrates how to define a conditional format in markup:

xaml
<dxg:TableView.FormatConditions>
   <dxg:IconSetFormatCondition FieldName="Profit" PredefinedFormatName="Stars3IconSet"/>
</dxg:TableView.FormatConditions>

The code sample below illustrates how to define the same conditional format in code-behind:

csharp
var profitFormatCondition = new IconSetFormatCondition() {
   FieldName = "Profit",
   PredefinedFormatName = "Stars3IconSet"
};
view.FormatConditions.Add(profitFormatCondition);
vb
Dim profitFormatCondition = New IconSetFormatCondition() With {
   .FieldName = "Profit",
   .PredefinedFormatName = "Stars3IconSet"
}
view.FormatConditions.Add(profitFormatCondition)

Define Custom Rules and Icons

The IconSetFormatCondition chooses the icon based on the minimum and maximum values from your data source. You can use the IconSetFormatCondition.MinValue and IconSetFormatCondition.MaxValue properties to define custom minimum and maximum values for the format condition:

xaml
<dxg:TableView.FormatConditions>
   <dxg:IconSetFormatCondition FieldName="SatisfactionScore" 
                               PredefinedFormatName="Quarters5IconSet" 
                               MinValue="0" MaxValue="1"/>
</dxg:TableView.FormatConditions>
<!-- ... -->
<dxg:GridColumn FieldName="SatisfactionScore">
   <dxg:GridColumn.EditSettings>
      <dxe:TextEditSettings MaskType="Numeric"
                            Mask="p0"
                            MaskUseAsDisplayFormat="True"/>
   </dxg:GridColumn.EditSettings>
</dxg:GridColumn>

The IconSetFormat object allows you to specify custom icons and display rules:

xaml
<dxg:IconSetFormatCondition FieldName="Profit">
   <dxg:IconSetFormatCondition.Format>
      <dx:IconSetFormat ElementThresholdType="Number">
         <dx:IconSetElement Threshold="-20" Icon="{dx:IconSet Name=RedToBlack4_1}"/>
         <dx:IconSetElement Threshold="0" Icon="{dx:IconSet Name=Stars3_2}"/>
         <dx:IconSetElement Threshold="10" Icon="{dx:IconSet Name=Stars3_1}"/>
      </dx:IconSetFormat>
   </dxg:IconSetFormatCondition.Format>
</dxg:IconSetFormatCondition>

Use the IconSetElement.Threshold property to specify the minimum value required to display the icon. The IconSetFormat.ElementThresholdType property specifies whether this value is a number or a percent relative to the minimum and maximum values in your data source.

Add Rules Using Conditional Formatting Menu

  1. Select the Icon Sets item in the Conditional Formatting Menu.
  2. Choose the required icon set in the invoked sub menu. The conditional formatting menu allows you to use only predefined Icon Sets formats stored within the TableView.PredefinedIconSetFormats (or TreeListView.PredefinedIconSetFormats) collection.

Add Rules Using Conditional Formatting Rules Manager

  1. Click the New Rule… in the Conditional Formatting Rules Manager.

  2. Select the Format all cells based on their values rule type in the invoked New Formatting Rule dialog.

  3. Select the Icon Sets format style.

  4. Specify the Type and Value fields. These options determine the conditional values on which cells/rows formatting is based.

  5. Specify the Min Value and Max Value fields to determine the range of values used in formatting. These fields are optional. The range is calculated automatically based on maximum and minimum values from cell values by default.

  6. Specify the Icon Style field to define the visual appearance of the applied rule. You can set the rules to determine how to display icons.

See Also

Conditional Formats

Conditional Formatting Menu

Conditional Formatting Rules Manager