简体   繁体   中英

GTK# Treeview - how to sort child nodes

I have a Gtk.TreeView with child nodes like this image (I have covered up text for employer proprietary reasons):

http://i.stack.imgur.com/kKemI.png

Sorting by the "Title" column (clicking on the column header) sorts by the 3 parent nodes, when I really just want it to sort all the children under each parent node. Is this possible?

Note that sorting by the "Path" column sorts the children nodes appropriately; I think because the parent nodes do not have text in that column. So I'm hoping there's a (easy?) way around the text in the Title column for the parent nodes.

Sorting is a little bit complex because you need to have several parts of your code (the model and the columns) to cooperate. To sort on a specific column that's what you need to do:

  1. Create a column (no shortcuts) and assign a value to the SortColumnId attribute. To keep it simple I usually assign the ordinal id of the column, starting from 0, ie, the first column in the view is 0, the second 1 and so on.
  2. Wrap your model in a Gtk.TreeModelSort
  3. Call SetSortFunc on the new model once for very column you want to sort and pass the column id you set in (1) as first argument. Make sure to match all column ids.

How the rows are sorted depends on the delegate you use as second argument to SetSortFunc . You get the model and two iters and you can do almost anything, even sort on multiple columns (with the two iters you can grab any value from the model, not just the values shown in the sorted column.)

Here is a simple example:

class MainClass
{
public static void Main (string[] args)
{
        Application.Init ();
        var win = CreateTreeWindow();
        win.ShowAll ();
        Application.Run ();
    }

    public static Gtk.Window CreateTreeWindow()
    {
        Gtk.Window window = new Gtk.Window("Sortable TreeView");

        Gtk.TreeIter iter;
        Gtk.TreeViewColumn col;
        Gtk.CellRendererText cell;

        Gtk.TreeView tree = new Gtk.TreeView();

        cell = new Gtk.CellRendererText();
        col = new Gtk.TreeViewColumn();
        col.Title = "Column 1";            
        col.PackStart(cell, true);
        col.AddAttribute(cell, "text", 0);
        col.SortColumnId = 0;

        tree.AppendColumn(col);

        cell = new Gtk.CellRendererText();
        col = new Gtk.TreeViewColumn();
        col.Title = "Column 2";            
        col.PackStart(cell, true);
        col.AddAttribute(cell, "text", 1);

        tree.AppendColumn(col);

        Gtk.TreeStore store = new Gtk.TreeStore(typeof (string), typeof (string));
        iter = store.AppendValues("BBB");
        store.AppendValues(iter, "AAA", "Zzz");
        store.AppendValues(iter, "DDD", "Ttt");
        store.AppendValues(iter, "CCC", "Ggg");

        iter = store.AppendValues("AAA");
        store.AppendValues(iter, "ZZZ", "Zzz");
        store.AppendValues(iter, "GGG", "Ggg");
        store.AppendValues(iter, "TTT", "Ttt");

        Gtk.TreeModelSort sortable = new Gtk.TreeModelSort(store);
        sortable.SetSortFunc(0, delegate(TreeModel model, TreeIter a, TreeIter b) {
            string s1 = (string)model.GetValue(a, 0);
            string s2 = (string)model.GetValue(b, 0);
            return String.Compare(s1, s2);
        });

        tree.Model = sortable;

        window.Add(tree);

        return window;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM