[Gtk-sharp-list] TreeView question

Peter Johanson latexer at gentoo.org
Thu Feb 15 16:24:38 EST 2007

On Thu, Feb 15, 2007 at 05:19:10PM +0200, Petteri Kautonen wrote:
> Hi,
> I need to create a TreeView that displays string-boolean (toggle) pairs 
> with no horizontal limit, so it would look like this:
> Value, bool
> Value, bool
>     Value, bool
>        Value, bool
>     Value, bool
> Value, bool
> Is this even possible and if it is, a simple example would be nice.

Attached is a simple example written in Boo (a .NET language, so it's
actually using the gtk# binding, but not from C#). The example uses
"attributes" to map attributes on the CellRenderer to values in the
given row for rendering. The alternate approach is using
TreeCellDataFunc to set the properties on a CellRenderer based on the
row's values, before the renderer then actually renders things.

Hope this helps,


Peter Johanson
<latexer at gentoo.org>
-------------- next part --------------

import System
import Gtk from 'gtk-sharp'
import GLib from 'glib-sharp'

# Usual basic app foo.
Application.Init ()
w = Window ('test')
w.DeleteEvent += { Application.Quit () }

# Our TreeView
tv = TreeView ()

# Define the format of our store, and put in bogus values.
store = ListStore (typeof (string), typeof (bool))
store.AppendValues ("Pete J", true)
store.AppendValues ("Bruce Lee", false)
store.AppendValues ("John Wayne", false)

# Set the TreeView's TreeModel to be our ListStore.
tv.Model = store 

# This the overload that uses 'attributes' to provide data to the renderer.
# It maps a column number in the store (the 0 part) to a GObject property 
# on the renderer. So for each row it renders, it sets the 'text' property on
# our CellRendererText c to be equal to the value of the 0th column of that row.
c = CellRendererText ()
tv.AppendColumn ("Name", c, 'text', 0)

toggle_c = CellRendererToggle ()
toggle_c.Toggled += def (o, args as ToggledArgs):
	iter as TreeIter
	if store.GetIter (iter, TreePath (args.Path)):
		store.SetValue (iter, 1, not toggle_c.Active)

tv.AppendColumn ("Enabled", toggle_c, 'active', 1)

sw = ScrolledWindow ()
sw.Add (tv)
w.Add (sw)
w.ShowAll ()

# Do it to it!
Application.Run ()

More information about the Gtk-sharp-list mailing list