[Gtk-sharp-list] Treeview question (controlling the model using a TreeModel)
Milton Pividori
miltondp at gmail.com
Thu Jan 3 13:26:47 EST 2008
Here is an example. I think I understood your problem:
------
using System;
using Gtk;
public partial class MainWindow: Gtk.Window
{
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
Gtk.TreeViewColumn artistColumn = new Gtk.TreeViewColumn ();
artistColumn.Title = "Artist";
Gtk.CellRendererText artistNameCell = new Gtk.CellRendererText ();
artistColumn.PackStart (artistNameCell, true);
Gtk.TreeViewColumn songColumn = new Gtk.TreeViewColumn ();
songColumn.Title = "Song Title";
Gtk.CellRendererText songTitleCell = new Gtk.CellRendererText ();
songColumn.PackStart (songTitleCell, true);
tree.AppendColumn (artistColumn);
tree.AppendColumn (songColumn);
artistColumn.SetCellDataFunc(artistNameCell,
new TreeCellDataFunc(this.RenderArtistName));
songColumn.SetCellDataFunc(songTitleCell,
new TreeCellDataFunc(this.RenderSongTitle));
/* I save the song and genre */
Gtk.TreeStore musicListStore = new Gtk.TreeStore (typeof (Song),
typeof(string));
Gtk.TreeIter iter = musicListStore.AppendValues(null, "Dance");
musicListStore.AppendValues (iter, new Song("Fannypack",
"Nu Nu (Yeah Yeah) (double j and haze radio edit)"));
iter = musicListStore.AppendValues (null, "Hip-hop");
musicListStore.AppendValues (iter,
new Song("Nelly", "Country Grammer"));
tree.Model = musicListStore;
}
private void RenderArtistName (Gtk.TreeViewColumn column,
Gtk.CellRenderer cell, Gtk.TreeModel model,
Gtk.TreeIter iter)
{
Song song = model.GetValue (iter, 0) as Song;
/* If song is null, then this is the parent iter */
if (song == null) {
string s = model.GetValue(iter, 1).ToString();
(cell as Gtk.CellRendererText).Text = s;
}
else
(cell as Gtk.CellRendererText).Text = song.Artist;
}
private void RenderSongTitle (Gtk.TreeViewColumn column,
Gtk.CellRenderer cell, Gtk.TreeModel model,
Gtk.TreeIter iter)
{
Song song = model.GetValue (iter, 0) as Song;
if (song == null)
(cell as Gtk.CellRendererText).Text = "";
else
(cell as Gtk.CellRendererText).Text = song.Title;
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
}
public class Song
{
public Song (string artist, string title)
{
this.Artist = artist;
this.Title = title;
}
public string Artist;
public string Title;
}
------
Is this what you want to do?
On Wed, 2008-01-02 at 16:56 +0000, Paul Bourke wrote:
> I pasted the code badly.. maybe I can simplify my question a little as
> would really like to get this working:
> Basically if you check out the piece of the tutorial here:
> http://www.mono-project.com/GtkSharp_TreeView_Tutorial#Controlling_how_the_model_is_used
> it describes how to set up a TreeView so that it is bound to an
> object containing your data. When the data in your object changes, so
> does the TreeView thus allowing you to update the data easily.
> My problem is that the example uses a ListModel rather than a
> TreeModel as in the previous section
> (http://www.mono-project.com/GtkSharp_TreeView_Tutorial#Your_first_TreeView).
> If anyone has any examples of how to do this above using a TreeModel I
> would greatly appreciate it.
>
> On 31/12/2007, Michael Hutchinson <m.j.hutchinson at gmail.com> wrote:
> > On Dec 30, 2007 6:01 PM, Paul Bourke <pauldbourke at gmail.com> wrote:
> > ...
> > > System.InvalidCastException: Cannot cast from source type to destination type.
> > > at GRapid.MainWindow.RenderURLColumn (Gtk.TreeViewColumn column,
> > ...
> > > TreeStore myTreeStore = new TreeStore (typeof(string), typeof (MyClass));
> > ...
> > The two store columns have type string and MyClass,
> >
> > > DownloadItem d = (DownloadItem) model.GetValue (iter, 0);
> >
> > but you're trying to cast the string to a DownloadItem.
> >
> > --
> > Michael Hutchinson
> > http://mjhutchinson.com
> >
> _______________________________________________
> Gtk-sharp-list maillist - Gtk-sharp-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/gtk-sharp-list
--
Milton Pividori
Blog: http://www.miltonpividori.com.ar
Jabber ID: milton.pividori at jabberes.org
GnuPG Public Key: gpg --keyserver wwwkeys.pgp.net --recv-key 0x663C185C
More information about the Gtk-sharp-list
mailing list