[Gtk-sharp-list] different color rows for GtkTreeView

Adam Tauno Williams adam at morrison-ind.com
Sun Apr 27 08:47:59 EDT 2008


> I think this is appropriate material for this list. I'm trying to have
> a TreeView with a ListStore have rows that are different colors. I'm
> pretty sure it's possible, because it looks like it's the case with
> MonoDevelop s and User Tasks/Comments. The question is, how?
> (Point of clarity: First row be red, second row be green -- that's
> all. I guess more generally, how do you control the row color as
> opposed to the column color)

You can set the colors and styles used by the cells of the list.  See
the Effect(...) method in
<http://code.google.com/p/consonance/source/browse/trunk/Whitemice.ZOGI.Gtk/General/Lists/SimpleTaskList.cs>

Note: GUI is just a static class that can store colors for keys,
results of ColorForKey is a Gdk.Color.

So far the only way I can find to do this is to have a custom renderer
for each cell, setup like -

objectIdColumn = new Gtk.TreeViewColumn ();
...
cell = new Gtk.CellRendererText ();
objectIdColumn.PackStart(cell, true);
objectIdColumn.SetCellDataFunc(cell, new Gtk.TreeCellDataFunc(RenderObjectId));
view.AppendColumn(objectIdColumn);

 - and RenderObjectId looks like -

protected virtual void RenderObjectId(
  Gtk.TreeViewColumn _column, 
  Gtk.CellRenderer _cell, 
  Gtk.TreeModel _model, 
  Gtk.TreeIter _iter)
{
  Task task = IterTask(_model, _iter); // just returns the object for this iter
   if(task == null)
     return;
   (_cell as Gtk.CellRendererText).Text = task.ObjectId.ToString();
   Effect(_model, _iter, (_cell as Gtk.CellRendererText), task);
}

- calling Effect(...) applies the effect based on row number [odd vs. even], etc...

It is a code-heavy solution but it works very well and is the only I've found to really control the look of the view.
<http://docs.opengroupware.org/Members/whitemice/consonance/ConsonanceTaskWindow/image_view_fullscreen>



More information about the Gtk-sharp-list mailing list