代码之家  ›  专栏  ›  技术社区  ›  TwentyMiles

如何使用f_创建多级树视图?

  •  4
  • TwentyMiles  · 技术社区  · 15 年前

    我想通过f_显示一个使用gtk widgets的目录结构,但是我很难理解如何将树视图转换为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)"|])
    

    请注意,列是作为数组显式传递的。

    最后,您可以使用append值返回的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 回复  |  直到 12 年前
        1
  •  5
  •   gradbot    15 年前

    我不确定你在找什么,但这里有一个翻译的例子 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()