[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
(http://www.mono-project.com/GtkSharp_TreeView_Tutorial#Controlling_how_the_model_is_used)
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,
(http://www.mono-project.com/GtkSharp_TreeView_Tutorial#Creating_a_Tree)
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
/home/paul/Projects/GRapid/GRapid/MainWindow.cs:113
  at (wrapper delegate-invoke)
System.MulticastDelegate:invoke_void_TreeViewColumn_CellRenderer_TreeModel_TreeIter
(Gtk.TreeViewColumn,Gtk.CellRenderer,Gtk.TreeModel,Gtk.TreeIter)
  at GtkSharp.TreeCellDataFuncWrapper.NativeCallback (IntPtr
tree_column, IntPtr cell, IntPtr tree_model, IntPtr iter, IntPtr data)
[0x00000]
. . . .

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.

<code>
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;
}
</code>


More information about the Gtk-sharp-list mailing list