2

我需要将诸如 DataGrid 之类的对象添加到我的 Gtk# Forms 中。我已经阅读了一些关于它的信息并找到了下一个代码:

    protected ListStore SetupModel(TreeView aTreeView, String StartOfColumns, params Type[] TypesOfColumnsForListStore)
{
    ListStore m = new ListStore(TypesOfColumnsForListStore);

    CellRendererText textCell=new CellRendererText();
    textCell.Editable=true;


    for(Int32 i=1;i<=TypesOfColumnsForListStore.Length;i++)
    {
        TreeViewColumn nameCol = new TreeViewColumn( StartOfColumns + i, textCell, "text", 0);
        //TreeViewColumn nameCol = new TreeViewColumn( StartOfColumns + i, new CellRendererText(), "text", 0);
        aTreeView.AppendColumn( nameCol);
    }




    aTreeView.Model = m;

    return m;
}

void PopulateData( ListStore model ) 
{
    //model.Append();
    model.AppendValues( "Fred", "Blue" );
    model.AppendValues( "Bob", "Green" );
    model.AppendValues( "Mary", "Yellow" );
    model.AppendValues( "Alice", "Red" );

}

但是如果我使用这段代码,所有的列都是只读的。然后我在这里找到了其他信息,但是下一个代码也不起作用:

protected void OnButton1Clicked (object sender, EventArgs e)    
{

    CellRendererText renderer=new CellRendererText();       TreeViewColumn
 treeColumn=new TreeViewColumn();       renderer.Height=40;
        renderer.Width=90;

        treeColumn.SetCellDataFunc (renderer, delegate (TreeViewColumn
 col, CellRenderer cell, TreeModel model, TreeIter iter)        {
            var textCell = (CellRendererText) cell;
            textCell.Text = (string) model.GetValue (iter, 0);
            textCell.Editable = (bool) model.GetValue (iter, 4);        
});

        treeview2.AppendColumn(treeColumn);

        ListStore model2=new ListStore(typeof(String));
        model2.AppendValues("1");       model2.AppendValues("2");

        treeview2.Model=model2;     
}

有人可以向我解释如何将 TreeView 用作可编辑的 DataGrid 吗?

4

1 回答 1

0

一旦你进入 TreeView,Gtk# 就相当复杂。

这是只读和可编辑列的代码。

        var tvTable = new Gtk.TreeView();
        tvTable.EnableGridLines = TreeViewGridLines.Both;

        // Create liststore
        var types = new System.Type[ 2 ];
        types[ 0 ] = typeof( string );
        types[ 1 ] = typeof( string );
        Gtk.ListStore listStore = new Gtk.ListStore( types );
        tvTable.Model = listStore;

        // Create index column
        var column = new Gtk.TreeViewColumn();
        var cell = new Gtk.CellRendererText();
        column.Title = "#";
        column.PackStart( cell, true );
        cell.Editable = false;
        cell.Foreground = "black";
        cell.Background = "light gray";
        column.AddAttribute( cell, "text", 0 );
        tvTable.AppendColumn( column );

        // Data column
        var column = new Gtk.TreeViewColumn();
        column.Expand = true;
        cell = new Gtk.CellRendererText();
        column.Title = document.Headers[ columnNumber ];
        column.PackStart( cell, true );
        cell.Editable = true;
        column.AddAttribute( cell, "text", columnNumber + 1 );
        cell.Edited += OnTreeViewCellEdited;
        tvTable.AppendColumn( column );

        // Add data
        listStore.AppendValues( new string[] { "1", "hello" } );

希望这可以帮助。

于 2015-05-08T06:02:45.030 回答