gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:10:25 27 4
gpt4 key购买 nike

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

Directory1
SubDirectory1
SubDirectory2
SubSubDirectory1
SubDirectory3
Directory2

如何使用 F# 通过 Gtk# 小部件显示此树结构?

编辑:

gradbot 是我所希望的答案,但有几个异常(exception)。如果您使用 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 Values 返回的 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

最佳答案

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

alt text
我认为多级 TreeView 的关键是将值附加到值,iter 在这一行 musicListStore.AppendValues (iter, "Fannypack", "Nu Nu (Yeah Yeah) (双 j 和雾霾 radio 编辑)") |> 忽略

// 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()

关于f# - 如何使用 F# 创建多级 TreeView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2451929/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com