Search code examples
c#treeviewmonodevelopgtk#

MonoDevelop / GtkSharp - How to add item to the beginning of the list in TreeView?


Is possible to add item to GtkSharp TreeView on the top of list/tree?
This is possible in Windows Form for example by this way:
listBox.Items.Insert(0, "anyItem");

However I'm note able to find similar solution in GtkSharp.


Solution

  • Create a ListStore or TreeStore object and assign it to the TreeView's Model property. Then you can insert or add items using the ListStore or TreeStore object.

    Below is a simple example that uses a ListStore.

    var listView = new TreeView ();
    listView.HeadersVisible = false;
    
    listStore = new ListStore (typeof(string));
    listView.Model = listStore;
    
    var cellView = new CellRendererText ();
    var column = new TreeViewColumn ("Title", cellView);
    column.AddAttribute (cellView, "text", 0);
    listView.AppendColumn (column);
    

    Then you can insert an item using:

     int position = 0;
     listStore.InsertWithValues (position, "MyItem");