Synopsis

This is a simple demo program showing how to set and unset the background color on individual rows in a treeview, so that you can, for example, highlight certain entries. You could also use it to create custom banding, etc.

Description

The trick here is to use a column in the model which does not appear in the view to store a color value. Treeview columns can hold any datatype, so we make the column a TreeModelColumn<Gdk::Color>, then set the property_cell_background_gdk for each column in the view to use this hidden column from the model as it's value (see line 24). This trick can be used for a lot of similar purposes, eg, if you have a column which is a CellRendererProgress, you set it's "value" attribute to an undisplayed int column and you can also replace the text label by setting it's "text" value to another undisplayed string column.

The demo works like this: when you double click on a row, it will be left highlighted in red (see line 39). When you double click it again, it will revert back to the normal background color. In order to do that, we need to have a record of what that "default" color is (see line 9).