[Gtk-sharp-list] Gtk.CellRendererText.Markup

Michael Hutchinson m.j.hutchinson at gmail.com
Mon Aug 25 14:03:06 EDT 2008

On Sun, Aug 24, 2008 at 3:12 AM, innominate <natenate at gmail.com> wrote:
> Ive figured it out. I was doing it wrong.
> For those who come after me:
> The file attached will work with some small changes
> 1 delete nameCell.Markup line
> 2 change mainColumn.AddAttribute (nameCell, "text", 2); to
> mainColumn.AddAttribute (nameCell, "markup", 2);
> 3 add the markup to list.AppendValues (true, iconPixBuf, "blabla\nline two
> bla bla bla...");

As you've probably worked out, the AddAttribute method of a column
adds property "mappings" that are used to assign properties to a
cellrenderer each time it's used to render the cell for a given view
column/row. These just assign the value from a corresponding row in
the TreeStore column number you specify. The property names are the
underlying GObject properties, not the GTK# properties, and as such
are names a little differently (usually just casing). You can find the
names in the GTK+ docs.

Note that a cellrenderer instance can be re-used for as many columns
as you want, though this has the downside that you may need to
reassign more properties to counteract properties that were set for
other columns.

A much more powerful alternative to property mappings is to use a
TreeCellDataFunc, for example:

col.SetCellDataFunc (textRenderer, delegate (TreeViewColumn
treeColumn, CellRenderer cell, TreeModel treeModel, TreeIter iter) {
    SomeObject val = (SomeObject) treeModel.GetValue (iter,
    CellRendererText textCell = (CellRendererText) cell;
    cell.Markup = string.Format ("<b>{0}</b>\n{1}", GLib.Markup.Escape
(val.SomeProp), GLib.Markup.Escape (val.SomeOtherProp));
    cell.Style = val.SomeIntProp > Pango.Style.Normal : pango.Style.Italic;

This means that you can derive more complex values on the fly, or for
example access properties of .NET properties,  rather than having to
precalculate every property value and put it in the TreeStore.

Michael Hutchinson

More information about the Gtk-sharp-list mailing list