1

I am currently working with mono and gtk#. Regarding the TreeView there is this Tutorial. I want to achieve the same thing thats presented under "Controlling how the model is used" So i have the Song Class and the Render-Methods to display artist and title.

But I want to display it via a TreeStore instead of a ListStore. So that I have a Rootnode for each Letter and under this node all Artists starting with this letter should be displayed.

My problem how can I add these RootNodes to the TreeStore? And where should I add them?

songs.Add(new Song("Dancing Djs vs. Roxette", "Fading like a flower"));
songs.Add(new Song("Xaiver","give me the right"));
songs.Add(new Song("Daft Punkt","Technologic"));

TreeStore musicListStore = new TreeStore(typeof(Song));
foreach (var s in songs) 
{
    musicListStore.AppendValues(s);
}

treeview1.Model = musicListStore;

treeview1.AppendColumn("Artist", new CellRendererText(),new TreeCellDataFunc(RenderArtistName));
treeview1.AppendColumn("Title", new CellRendererText(),new TreeCellDataFunc(RenderSongTitle));

private void RenderArtistName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
{
    Song s = model.GetValue(iter,0) as Song;
    (cell as CellRendererText).Text = s.Artist;
}

private void RenderSongTitle(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
{
    Song s = model.GetValue(iter,0) as Song;
    (cell as CellRendererText).Text = s.Title;
}

So i want to achieve that there are RootNodes for each letter and underneath each letter there should be all Artists listed starting with this letter.

My problem is how to add the letter to the TreeStore plus how do i know where to insert each Song then.

Here is a screenshot how i would like it to look like(I am not allowed to upload them directly. So i Had to use an external hosted. Sry): Screenshot

gpoo
  • 8,408
  • 3
  • 38
  • 53
Chris
  • 21
  • 4

1 Answers1

2

You can build up your tree quite easily. Eg;

var store = new Gtk.TreeStore( typeof(string) );

// add a root node
var root = store.AppendValues("hello"); 

// add a child of the root
store.AppendValues(root,"world");

// add another child
var mono = store.AppendValues(root,"mono");

// add a second level child
store.AppendValues(mono,"gtk");

So, in the context of your music app..

// title, artist
var store = new Gtk.TreeStore( typeof(string), typeof(string) );

// make an index of top level nodes using thier TreeIters
var index = new Dictionary<string,Gtk.TreeIter>();

// add index nodes
foreach ( var letter in new List<string>{ "A", "B", "C" ... "Z" } ){
    index[letter.ToLower()] = store.AppendValues( letter );
}

// add songs
foreach ( var song in songlist ){
    var title = song.Title;
    var artist = song.Artist;

    var first = title.SubString(0,1).ToLower();
    var iter = store[first];

    // add this song
    store.AppendValues( iter, title, artist );
}

You will have to do some extra work if you want to dynamically add index nodes, each time you add a node at a level all your treeiter values at that level or deeper become worthless.

IanNorton
  • 7,145
  • 2
  • 25
  • 28
  • Thanks, I know and have already used it that way. My TreeStore is of type Song and i use TreeCellDataFunc. here is my code: – Chris Mar 21 '12 at 07:14
  • Thanks. This works that way. It is Gtk.TreeStore( typeof(string), typeof(string) ) but i would prefer using typeof(Song) because i have several columns hooked up with TreeCellDataFunc and i do a bunch of more stuff. Like setting a pixbuf, changing color, crossing out etc. So this is all done inside my TreeCellDataFunc. Which is kinda cool because i dont have to build up the whole Treestore on my own. So basically i was asking if there is a conveniant way using typeof(Song) and display data hierarchically – Chris Apr 03 '12 at 14:49