2010-03-16 31 views
4

我想通过F#显示使用Gtk#小部件的目录结构,但是我很难弄清楚如何将TreeViews转换为F#。说我看起来像这样的目录结构:如何使用F#创建多级TreeView?

Directory1 
    SubDirectory1 
    SubDirectory2 
    SubSubDirectory1 
    SubDirectory3 
Directory2 

我将如何显示此树结构,使用F#Gtk#的小工具?

编辑:

gradbot的是我用几个例外的希望的答案。如果你使用ListStore,你失去了扩大水平的能力,如果你改用:

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

你得到扩张水平的布局。这样做,但是,打破以AppendValues来电,所以你必须添加一些线索让编译器找出哪些重载的方法使用:

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

注意,列被明确作为数组传递。

最后,你可以嵌套的水平进一步通过使用附加的返回值ListIter

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追加值值,iter,与答案编辑你的问题。这是你的wiki。 :) – gradbot 2010-03-16 15:32:14

回答

5

我不完全相信你在找什么,但这里是从他们的tutorials翻译的例子。它可以帮助你开始。图片来自tutorial site

alt text http://www.mono-project.com/files/9/92/GtkSharpTreeViewTutorial-Tree1.png
我认为最关键的多层次的树状视图是在这一行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()