2010-03-16 3 views
4

Je voudrais afficher une structure de répertoire en utilisant les widgets Gtk # à travers F #, mais j'ai du mal à comprendre comment traduire TreeViews en F #. Dire que j'avais une structure de répertoire qui ressemble à ceci:Comment créer un TreeView à plusieurs niveaux en utilisant F #?

Directory1 
    SubDirectory1 
    SubDirectory2 
    SubSubDirectory1 
    SubDirectory3 
Directory2 

Comment puis-je montrer cette structure d'arbre avec Gtk # widgets en utilisant F #?

EDIT:

gradbot WAS la réponse que j'espérais avec quelques exceptions. Si vous utilisez ListStore, vous perdez la possibilité d'étendre les niveaux, si vous utilisez à la place:

let musicListStore = new Gtk.TreeStore([|typeof<String>; typeof<String>|]) 

vous obtenez une mise en page avec des niveaux extensibles. Faire cela, cependant, rompt les appels à AppendValues ​​de sorte que vous devez ajouter quelques indices pour le compilateur de savoir qui surcharge méthode à utiliser:

musicListStore.AppendValues (iter, [|"Fannypack" ; "Nu Nu (Yeah Yeah) (double j and haze radio edit)"|]) 

Notez que les colonnes sont passées explicitement comme un tableau.

Enfin, vous pouvez encore les niveaux de nid en utilisant le ListIter retourné par les valeurs Append

let iter = musicListStore.AppendValues ("Dance") 
let subiter = musicListStore.AppendValues (iter, [|"Fannypack" ; "Nu Nu (Yeah Yeah) (double j and haze radio edit)"|]) 
musicListStore.AppendValues (subiter, [|"Some Dude"; "Some Song"|]) |> ignore 
+0

+1, pour éditer votre question avec la réponse. C'est très wiki de vous. :) – gradbot

Répondre

5

Je ne suis pas sûr de ce que vous cherchez, mais voici un exemple traduit de leur tutorials. Cela peut vous aider à démarrer. Image prise de tutorial site.

alt text http://www.mono-project.com/files/9/92/GtkSharpTreeViewTutorial-Tree1.png
Je pense que la clé d'une arborescence à plusieurs niveaux consiste à ajouter des valeurs à des valeurs, iter dans cette ligne musicListStore.AppendValues (iter, "Fannypack", "Nu Nu (Yeah Yeah) (double j and haze radio edit)") |> ignore

// you will need to add these references gtk-sharp, gtk-sharp, glib-sharp 
// and set the projects running directory to 
// C:\Program Files (x86)\GtkSharp\2.12\bin\ 

module SOQuestion 

open Gtk 
open System 

let main() = 
    Gtk.Application.Init() 

    // Create a Window 
    let window = new Gtk.Window("TreeView Example") 
    window.SetSizeRequest(500, 200) 

    // Create our TreeView 
    let tree = new Gtk.TreeView() 
    // Add our tree to the window 
    window.Add(tree) 

    // Create a column for the artist name 
    let artistColumn = new Gtk.TreeViewColumn() 
    artistColumn.Title <- "Artist" 

    // Create the text cell that will display the artist name 
    let artistNameCell = new Gtk.CellRendererText() 
    // Add the cell to the column 
    artistColumn.PackStart(artistNameCell, true) 

    // Create a column for the song title 
    let songColumn = new Gtk.TreeViewColumn() 
    songColumn.Title <- "Song Title" 

    // Do the same for the song title column 
    let songTitleCell = new Gtk.CellRendererText() 
    songColumn.PackStart(songTitleCell, true) 

    // Add the columns to the TreeView 
    tree.AppendColumn(artistColumn) |> ignore 
    tree.AppendColumn(songColumn) |> ignore 

    // Tell the Cell Renderers which items in the model to display 
    artistColumn.AddAttribute(artistNameCell, "text", 0) 
    songColumn.AddAttribute(songTitleCell, "text", 1) 

    let musicListStore = new Gtk.ListStore([|typeof<String>; typeof<String>|]) 

    let iter = musicListStore.AppendValues ("Dance") 
    musicListStore.AppendValues (iter, "Fannypack", "Nu Nu (Yeah Yeah) (double j and haze radio edit)") |> ignore 

    let iter = musicListStore.AppendValues ("Hip-hop") 
    musicListStore.AppendValues (iter, "Nelly", "Country Grammer") |> ignore 

    // Assign the model to the TreeView 
    tree.Model <- musicListStore 

    // Show the window and everything on it 
    window.ShowAll() 

    // add event handler so Gtk will exit 
    window.DeleteEvent.Add(fun _ -> Gtk.Application.Quit()) 

    Gtk.Application.Run() 

[<STAThread>] 
main() 
Questions connexes