Search code examples
c#treeviewmonodevelopgtk#

Create a Gtk.TreeView from a flat file list GTK C#


I am wondering if any one could help me I am just learning Gtk and c# and I am finding it hard to find an example which shows how to create a TreeView from a flat list of files.

            var paths = new List<string>
                    {
                        @"C:\WINDOWS\AppPatch\MUI\040C",
                        @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
                        @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI",
                        @"C:\WINDOWS\addins",
                        @"C:\WINDOWS\addins\file1.f",
                        @"C:\WINDOWS\addins\file2.f",
                        @"C:\WINDOWS\addins\file3.f",
                        @"C:\WINDOWS\AppPatch",
                        @"C:\WINDOWS\AppPatch\MUI",
                        @"C:\WINDOWS\AppPatch\hello.JPG",
                        @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409"
                    };

Then I am then trying to put them into a hierarchical form and put them into a tree, I am unsure how to create tree from the flat paths.

    private static void FillTree(IEnumerable<string> paths)
    {
        FileTreeView = new Gtk.TreeView();
        Add(FileTreeView);

        Gtk.TreeViewColumn Column = new Gtk.TreeViewColumn();

        string subPathA;
        foreach (string path in paths)
        {
            subPathAgg = string.Empty;
            var builder = new System.Text.StringBuilder();
            builder.Append(subPathAgg);
            foreach (string subPath in path.Split(@"\"))
            {
                builder.Append(subPath + @"\");

                Console.WriteLine(subPath + @"\");
            }
            subPathAgg = builder.ToString();
        }
    }

Solution

  • TreeView is a powerful widget, in my opinion a too powerful mixing. I wrote a GtkUtil module that contains the GtkTableTextView class, which makes it easier to user the TreeView.

    With this class, you will be able to create the table as follows:

    var tvTable = new Gtk.TreeView();
    this.Add( tvTable );
    
    var Headers = new string[] { "#", "Path" };
    var ttTable = new GtkUtil.TableTextView( this.tvTable, Headers.Count, Headers.Count );
    ttTable.Headers = Headers;
    
    foreach(string path in paths) {
        ttTable.AppendRow();
        ttTable.Set( i, 1, path );
    }
    
    this.ShowAll();
    

    If you still prefer to do it without a library, then you should follow the standard tutorial about treeview.

    var tree = new Gtk.TreeView ();
    this.Add( tree );
    
    // Create a column for the file path
    Gtk.TreeViewColumn pathColumn = new Gtk.TreeViewColumn ();
    pathColumn.Title = "Path";
    tree.appendColumn( pathColumn );
    
    // Create an appropriate model
    var pathListStore = new Gtk.ListStore( typeof( string ) );
    tree.Model = pathListStore;
    
    // Add the data
    foreach(string path in paths) {
        tree.AppendValues( path );
    }
    
    this.ShowAll();
    

    Hope this helps.