[Gtk-sharp-list] Treeview question (controlling the model using a TreeModel)

Paul Bourke pauldbourke at gmail.com
Sun Dec 30 18:01:52 EST 2007

I have being working on a gtk-sharp app based heavily around the
manipulation of a Treeview widget.
I have being following the example at
to hold the data I need to display in a class, so when I manipulate
the data the Treeview should be updated too.  My problem is that I
want to use a TreeModel as in the previous section,
rather than a ListModel.  When I try to make use of the code for the
TreeModel I get the following exception:

System.InvalidCastException: Cannot cast from source type to destination type.
  at GRapid.MainWindow.RenderURLColumn (Gtk.TreeViewColumn column,
Gtk.CellRenderer cell, TreeModel model, TreeIter iter) [0x00000] in
  at (wrapper delegate-invoke)
  at GtkSharp.TreeCellDataFuncWrapper.NativeCallback (IntPtr
tree_column, IntPtr cell, IntPtr tree_model, IntPtr iter, IntPtr data)
. . . .

Below are some snippets of the relevant code, I would really
appreciate if someone could let me know how to use the above 'model
control' with a TreeModel rather than the ListModel.

TreeStore myTreeStore = new TreeStore (typeof(string), typeof (MyClass));
TreeViewColumn urlColumn = new TreeViewColumn();
urlColumn.Title = "URL";
CellRendererText urlCell = new CellRendererText();
urlColumn.PackStart (urlCell, true);
urlColumn.SetCellDataFunc (urlCell, new Gtk.TreeCellDataFunc (RenderURLColumn));

TreeIter iter = myTreeStore.AppendValues ("Title");
downloadViewStore.AppendValues (iter, new MyClass("column1", "column2"));

private void RenderURLColumn(TreeViewColumn column, CellRenderer cell,
TreeModel model, TreeIter iter)
    DownloadItem d = (DownloadItem) model.GetValue (iter, 0);
    (cell as Gtk.CellRendererText).Text = d.URL;

More information about the Gtk-sharp-list mailing list