Gtk#的DataGrid或如何使用TreeView作为DataGrid

时间:2012-11-17 02:28:56

标签: user-interface monodevelop gtk#

我需要将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" );

}

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

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吗?

1 个答案:

答案 0 :(得分: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" } );

希望这有帮助。