从平面文件列表创建 Gtk.TreeView GTK C#
Create a Gtk.TreeView from a flat file list GTK C#
我想知道是否有人可以帮助我,我只是在学习 Gtk 和 c#,我发现很难找到一个示例来说明如何从平面文件列表创建 TreeView。
var paths = new List<string>
{
@"C:\WINDOWS\AppPatch\MUI0C",
@"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
@"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI",
@"C:\WINDOWS\addins",
@"C:\WINDOWS\addins\file1.f",
@"C:\WINDOWS\addins\file2.f",
@"C:\WINDOWS\addins\file3.f",
@"C:\WINDOWS\AppPatch",
@"C:\WINDOWS\AppPatch\MUI",
@"C:\WINDOWS\AppPatch\hello.JPG",
@"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI09"
};
然后我试图将它们以分层形式放入树中,我不确定如何从平面路径创建树。
private static void FillTree(IEnumerable<string> paths)
{
FileTreeView = new Gtk.TreeView();
Add(FileTreeView);
Gtk.TreeViewColumn Column = new Gtk.TreeViewColumn();
string subPathA;
foreach (string path in paths)
{
subPathAgg = string.Empty;
var builder = new System.Text.StringBuilder();
builder.Append(subPathAgg);
foreach (string subPath in path.Split(@"\"))
{
builder.Append(subPath + @"\");
Console.WriteLine(subPath + @"\");
}
subPathAgg = builder.ToString();
}
}
TreeView是一个强大的widget,在我看来太强大了。
我写了一个包含 GtkTableTextView class 的 GtkUtil module,这使得使用 TreeView.
有了这个 class,您将能够创建 table,如下所示:
var tvTable = new Gtk.TreeView();
this.Add( tvTable );
var Headers = new string[] { "#", "Path" };
var ttTable = new GtkUtil.TableTextView( this.tvTable, Headers.Count, Headers.Count );
ttTable.Headers = Headers;
foreach(string path in paths) {
ttTable.AppendRow();
ttTable.Set( i, 1, path );
}
this.ShowAll();
如果您仍然喜欢在没有图书馆的情况下进行操作,那么您应该遵循 the standard tutorial about treeview。
var tree = new Gtk.TreeView ();
this.Add( tree );
// Create a column for the file path
Gtk.TreeViewColumn pathColumn = new Gtk.TreeViewColumn ();
pathColumn.Title = "Path";
tree.appendColumn( pathColumn );
// Create an appropriate model
var pathListStore = new Gtk.ListStore( typeof( string ) );
tree.Model = pathListStore;
// Add the data
foreach(string path in paths) {
tree.AppendValues( path );
}
this.ShowAll();
希望对您有所帮助。
我想知道是否有人可以帮助我,我只是在学习 Gtk 和 c#,我发现很难找到一个示例来说明如何从平面文件列表创建 TreeView。
var paths = new List<string>
{
@"C:\WINDOWS\AppPatch\MUI0C",
@"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
@"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI",
@"C:\WINDOWS\addins",
@"C:\WINDOWS\addins\file1.f",
@"C:\WINDOWS\addins\file2.f",
@"C:\WINDOWS\addins\file3.f",
@"C:\WINDOWS\AppPatch",
@"C:\WINDOWS\AppPatch\MUI",
@"C:\WINDOWS\AppPatch\hello.JPG",
@"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI09"
};
然后我试图将它们以分层形式放入树中,我不确定如何从平面路径创建树。
private static void FillTree(IEnumerable<string> paths)
{
FileTreeView = new Gtk.TreeView();
Add(FileTreeView);
Gtk.TreeViewColumn Column = new Gtk.TreeViewColumn();
string subPathA;
foreach (string path in paths)
{
subPathAgg = string.Empty;
var builder = new System.Text.StringBuilder();
builder.Append(subPathAgg);
foreach (string subPath in path.Split(@"\"))
{
builder.Append(subPath + @"\");
Console.WriteLine(subPath + @"\");
}
subPathAgg = builder.ToString();
}
}
TreeView是一个强大的widget,在我看来太强大了。 我写了一个包含 GtkTableTextView class 的 GtkUtil module,这使得使用 TreeView.
有了这个 class,您将能够创建 table,如下所示:
var tvTable = new Gtk.TreeView();
this.Add( tvTable );
var Headers = new string[] { "#", "Path" };
var ttTable = new GtkUtil.TableTextView( this.tvTable, Headers.Count, Headers.Count );
ttTable.Headers = Headers;
foreach(string path in paths) {
ttTable.AppendRow();
ttTable.Set( i, 1, path );
}
this.ShowAll();
如果您仍然喜欢在没有图书馆的情况下进行操作,那么您应该遵循 the standard tutorial about treeview。
var tree = new Gtk.TreeView ();
this.Add( tree );
// Create a column for the file path
Gtk.TreeViewColumn pathColumn = new Gtk.TreeViewColumn ();
pathColumn.Title = "Path";
tree.appendColumn( pathColumn );
// Create an appropriate model
var pathListStore = new Gtk.ListStore( typeof( string ) );
tree.Model = pathListStore;
// Add the data
foreach(string path in paths) {
tree.AppendValues( path );
}
this.ShowAll();
希望对您有所帮助。