如何使用F#创建多级TreeView?

时间:2010-03-16 03:22:55

标签: f# mono gtk# gtktreeview

我想通过F#使用Gtk #widgets显示目录结构,但我很难搞清楚如何将TreeViews转换为F#。假设我有一个如下所示的目录结构:

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

1 个答案:

答案 0 :(得分:5)

我不确定你在寻找什么,但这是他们tutorials的翻译示例。它可能会帮助您入门。图片取自tutorial site

alt text http://www.mono-project.com/files/9/92/GtkSharpTreeViewTutorial-Tree1.png
我认为多级树视图的关键是将值附加到此行中的值iter 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()