3

我正在使用 monodevelop 创建一个系统。我需要实现 Gtk TreeView 来显示数据。我已经按照此处的说明进行操作,但仍然无法正常工作。

我的问题是我正在课堂上生成我的树视图。这是我的代码:

在 MainWindow.cs

protected void OnShowCustomerTab (object sender, System.EventArgs e)
{
    customer.treeViewTable(customerTreeView);
}

在我的课上

public void treeViewTable(Gtk.TreeView tree)
    {
        tree = new Gtk.TreeView();
        tree.Hide();

        // generate tree column
        Gtk.TreeViewColumn lineNoColumn = new Gtk.TreeViewColumn();
        Gtk.CellRendererText lineNoCell = new Gtk.CellRendererText();
        lineNoColumn.Title = "#";
        lineNoColumn.PackStart(lineNoCell, true);
        lineNoColumn.AddAttribute(lineNoCell, "text", 0);

        Gtk.TreeViewColumn customerCodeColumn = new Gtk.TreeViewColumn();
        Gtk.CellRendererText customerCodeCell = new Gtk.CellRendererText();
        customerCodeColumn.Title = "Customer Code";
        customerCodeColumn.PackStart(customerCodeCell, true);
        customerCodeColumn.AddAttribute(customerCodeCell, "text", 1);

        // append the column and data on the tree table
        tree.AppendColumn(lineNoColumn);
        tree.AppendColumn(customerCodeColumn);

        tree.Show();
    }

我的代码有问题吗?请帮忙。提前致谢。

4

1 回答 1

0

在这里您手动创建自己的树视图,但您也可以使用设计器,然后确保命名您的树视图。这是一个示例 - 将其放在 MainWindow 中的 build() 之后:

    // Create a column for the name
    Gtk.TreeViewColumn nameColumn = new Gtk.TreeViewColumn ();
    nameColumn.Title = "Name";

    // Create a column for the song title
    Gtk.TreeViewColumn sColumn = new Gtk.TreeViewColumn ();
    sColumn.Title = "Song Title";

    // Add the columns to the TreeView
    this.<NameOfYourNodeView>.NodeSelection.NodeView.AppendColumn(nameColumn);
    this.<NameOfYourNodeView>.NodeSelection.NodeView.AppendColumn(sColumn);

    // Create a model that will hold two strings -  Name and Song Title
    Gtk.ListStore mListStore = new Gtk.ListStore (typeof (string), typeof (string));

    // Assign the model to the TreeView
    this.<NameOfYourNodeView>.NodeSelection.NodeView.Model = mListStore;

    // Add some data to the store
    mListStore.AppendValues ("Garbage", "Dog New Tricks");

    // Create the text cell that will display the artist name
    Gtk.CellRendererText NameCell = new Gtk.CellRendererText ();

    // Add the cell to the column
    nameColumn.PackStart (NameCell, true);

    // Do the same for the song title column
    Gtk.CellRendererText sTitleCell = new Gtk.CellRendererText ();
    sColumn.PackStart (sTitleCell, true);

    // Tell the Cell Renderers which items in the model to display
    nameColumn.AddAttribute (NameCell, "text", 0);
    sColumn.AddAttribute (sTitleCell, "text", 1);

结果(在您的树视图内):

Name     Song Title
Garbage  Dog New Tricks
于 2016-05-31T12:48:28.583 回答