我需要将诸如 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 吗?