Specifying Styles Using Custom Painting Events

  • 3 minutes to read

The custom painting events fire for each element when it needs to be painted. These events provide two opportunities. The first is to paint the element being processed manually, please refer to the Custom Painting Overview topic for details on how to implement this. The second is to change an element's appearance settings prior to it being painted. This topic describes how to perform this and for what purposes this can be used.

The Basics of Specifying Styles Using Custom Painting Events

The vertical grid controls (VGridControl and PropertyGridControl) provide a number of events that enable you to paint elements in a custom manner. Each event fires for elements of a specific type. Thus, the parameter sets of these events are not common since different information is required to paint different elements. However, some of the individual parameters are common. For instance, each custom painting event has parameters that specify the painted element's bounding rectangle, the Graphics object which represents the painting surface, etc. All events also have a common CustomDrawEventArgs.Appearance parameter, which lets you customize the appearance settings of the processed element.

Use the custom painting events to specify appearance settings only when it is necessary to assign them to individual elements of a certain type (when this cannot be performed by other means). For instance, you can use this technique to change a particular editor row's header style.

An example of using the custom painting events unnecessarily is to specify different styles for category rows. Because this can be performed using the rows' BaseRow.AppearanceCell properties and using the custom painting events, in this case, it would lead to a slight performance decrease.

Note: The custom painting events fire just prior to an element being painted. Thus, they have the highest priority so they override all other appearance settings.

Specifying Custom Styles for Row Headers - A Sample

The sample in this section shows how to change the appearance settings of an individual row headers. It handles the VGridControlBase.CustomDrawRowHeaderCell and VGridControlBase.CustomDrawRowHeaderIndent events to display headers of the Trademark and Model rows using a different style. The auxiliary InitAppearances method creates and customizes a custom appearance object which will be applied to the required elements. This method is called when the application starts. See the code below.


AppearanceObject customStyle;
private void InitAppearances() {
   customStyle = new AppearanceObject();
   customStyle.BackColor = Color.PeachPuff;
   customStyle.BackColor2 = Color.Orange;
   customStyle.GradientMode = System.Drawing.Drawing2D.LinearGradientMode.Vertical;
   customStyle.ForeColor = Color.SaddleBrown;
}

private void vGridControl1_CustomDrawRowHeaderCell(object sender, 
DevExpress.XtraVerticalGrid.Events.CustomDrawRowHeaderCellEventArgs e) {
   if (e.Caption != "Model" && e.Caption != "Trademark") return;
   if (!e.Focused) {
      e.Appearance.Combine(customStyle);
   }
}

private void vGridControl1_CustomDrawRowHeaderIndent(object sender, 
DevExpress.XtraVerticalGrid.Events.CustomDrawRowHeaderIndentEventArgs e) {
   if (!(e.Row is EditorRow)) return;
   EditorRow row = e.Row as EditorRow;
   if (row.Properties.Caption != "Model" && row.Properties.Caption != "Trademark") return;
   if (row != vGridControl1.FocusedRow) {
      e.Appearance.Combine(customStyle);
   }
}

The image below displays the result of handling events in such a manner.

Styles - CustomDraw - Headers