Combobox GTK CellRenderCombo将值设置到适当的单元格中

Combobox GTK CellRenderCombo将值设置到适当的单元格中,combobox,gtk,treecellrenderer,Combobox,Gtk,Treecellrenderer,我要做的是一个有多个栏目的树状视图。第一列是一个组合框,这意味着我使用Gtk.cellRenderCombo。问题是,当我从组合框中选择一个值时,我希望单元格中的文本从“”更改为我刚才选择的值。如果在Gtk.cellRenderCombo.Edited事件中,我将columns文本字段设置为EditedArgs.NewText,这是可行的 问题是,每次我设置一个值时,我都会创建一个新行,我希望文本字段的行为与Gtk.cellRenderText中的行为相同,但事实并非如此。对于该列的每一行,它不

我要做的是一个有多个栏目的树状视图。第一列是一个组合框,这意味着我使用Gtk.cellRenderCombo。问题是,当我从组合框中选择一个值时,我希望单元格中的文本从“”更改为我刚才选择的值。如果在Gtk.cellRenderCombo.Edited事件中,我将columns文本字段设置为EditedArgs.NewText,这是可行的

问题是,每次我设置一个值时,我都会创建一个新行,我希望文本字段的行为与Gtk.cellRenderText中的行为相同,但事实并非如此。对于该列的每一行,它不是不同的值,而是与Gtk.cellRenderCombo.Text中设置的值相同

CellRenderer不应该包含任何状态,所以从我的尝试来看,使用文本字段是一个非常糟糕的主意

但是如果我从Gtk.ListStore设置了一些值,这就是我的TreeView的模型(与Gtk.CellRenderCombo的模型不同)。设置的值永远不会显示在组合框的列中

class Program
{
    private static Gtk.TreeView treeview = null;

    static void OnEdited(object sender, Gtk.EditedArgs args)
    {
        Gtk.TreeSelection selection = treeview.Selection;
        Gtk.TreeIter iter;
        selection.GetSelected(out iter);

        treeview.Model.SetValue(iter, 0, args.NewText); // the CellRendererCombo
        treeview.Model.SetValue(iter, 1, args.NewText); // the CellRendererText

        //(sender as Gtk.CellRendererCombo).Text = args.NewText; // Will set all the Cells of this Column to the Selection's Text
    }

    static void Main(string[] args)
    {
        Gtk.Application.Init();

        Gtk.Window window = new Window("TreeView ComboTest");
        window.WidthRequest = 200;
        window.HeightRequest = 150;

        Gtk.ListStore treeModel = new ListStore(typeof(string), typeof(string));
        treeview = new TreeView(treeModel);

        // Values to be chosen in the ComboBox
        Gtk.ListStore comboModel = new ListStore(typeof(string));
        Gtk.ComboBox comboBox = new ComboBox(comboModel);
        comboBox.AppendText("<Please select>");
        comboBox.AppendText("A");
        comboBox.AppendText("B");
        comboBox.AppendText("C");
        comboBox.Active = 0;

        Gtk.TreeViewColumn comboCol = new TreeViewColumn();
        Gtk.CellRendererCombo comboCell = new CellRendererCombo();
        comboCol.Title = "Combo Column";
        comboCol.PackStart(comboCell, true);
        comboCell.Editable = true;
        comboCell.Edited += OnEdited;
        comboCell.TextColumn = 0;
        comboCell.Text = comboBox.ActiveText;
        comboCell.Model = comboModel;
        comboCell.WidthChars = 20;

        Gtk.TreeViewColumn valueCol = new TreeViewColumn();
        Gtk.CellRendererText valueCell = new CellRendererText();
        valueCol.Title = "Value";
        valueCol.PackStart(valueCell, true);
        valueCol.AddAttribute(valueCell, "text", 1);

        treeview.AppendColumn(comboCol);
        treeview.AppendColumn(valueCol);

        // Append the values used for the tests
        treeModel.AppendValues("comboBox1", string.Empty); // the string value setted for the first column does not appear.
        treeModel.AppendValues("comboBox2", string.Empty);
        treeModel.AppendValues("comboBox3", string.Empty);

        window.Add(treeview);
        window.ShowAll();
        Gtk.Application.Run();
    }
}
类程序
{
私有静态Gtk.TreeView TreeView=null;
静态void oneedid(对象发送方,Gtk.EditedArgs args)
{
Gtk.treeseselection selection=treeview.selection;
Gtk.TreeIter-iter;
选择。选择(退出iter);
treeview.Model.SetValue(iter,0,args.NewText);//CellRenderCombo
treeview.Model.SetValue(iter,1,args.NewText);//CellRenderText
//(发件人为Gtk.CellRenderCombo)。Text=args.NewText;//将此列的所有单元格设置为所选内容的文本
}
静态void Main(字符串[]参数)
{
Gtk.Application.Init();
Gtk.Window窗口=新窗口(“TreeView组合测试”);
window.WidthRequest=200;
window.HeightRequest=150;
Gtk.ListStore treeModel=新的ListStore(typeof(string),typeof(string));
treeview=新的treeview(treeModel);
//要在组合框中选择的值
Gtk.ListStore comboModel=新的ListStore(typeof(string));
Gtk.ComboBox ComboBox=新的ComboBox(comboModel);
comboBox.AppendText(“”);
组合框。追加文本(“A”);
组合框。追加文本(“B”);
组合框。追加文本(“C”);
comboBox.Active=0;
Gtk.TreeViewColumn comboCol=新的TreeViewColumn();
Gtk.cellRenderCombo comboCell=新的cellRenderCombo();
comboCol.Title=“组合列”;
comboCol.PackStart(comboCell,true);
comboCell.Editable=true;
comboCell.Edited+=oneedited;
comboCell.TextColumn=0;
comboCell.Text=comboBox.ActiveText;
comboCell.Model=comboModel;
comboCell.WidthChars=20;
Gtk.TreeViewColumn valueCol=新的TreeViewColumn();
Gtk.cellRenderText值cell=新的cellRenderText();
valueCol.Title=“Value”;
valueCol.PackStart(valueCell,true);
valueCol.AddAttribute(valueCell,“文本”,1);
treeview.AppendColumn(comboCol);
treeview.AppendColumn(valueCol);
//附加用于测试的值
treeModel.AppendValues(“comboBox1”,string.Empty);//为第一列设置的字符串值不会出现。
treeModel.AppendValues(“comboBox2”,string.Empty);
treeModel.AppendValues(“comboBox3”,string.Empty);
window.Add(treeview);
ShowAll();
Gtk.Application.Run();
}
}
我希望组合框的单元格中已经进行了选择,以显示值,但在以后的更改中继续可编辑

如果有人有办法做到这一点,我将非常感谢你的投入。谢谢

更新:


我的想法是,因为Gtk.cellRenderCombo继承了Gtk.cellRenderText,所以它忽略了在单元格中设置的值。现在,我想我可以创建一个自定义MyCellRenderCombo,该Combo继承自Gtk.CellRenderCombo,并使用为渲染提供的单元格中设置的值,但是关于Gtk.CellRenderText和Gtk.CellRenderCombo之间差异的文档非常少。。。我想我应该访问C中的实现以了解详细信息。

好的,我找到了解决问题的方法,我使用了一个“隐藏”列,在其中我读取了“文本”值。隐藏列包含Gtk.cellRenderText,Gtk.cellRenderCombo必须具有与新列的属性映射

生成的代码如下所示:

class Program
{
    private static Gtk.TreeView treeview = null;

    static void OnEdited(object sender, Gtk.EditedArgs args)
    {
        Gtk.TreeSelection selection = treeview.Selection;
        Gtk.TreeIter iter;
        selection.GetSelected(out iter);

        treeview.Model.SetValue(iter, 1, args.NewText); // the CellRendererText
    }

    static void Main(string[] args)
    {
        Gtk.Application.Init();

        Gtk.Window window = new Window("TreeView ComboTest");
        window.WidthRequest = 200;
        window.HeightRequest = 150;

        Gtk.ListStore treeModel = new ListStore(typeof(string), typeof(string));
        treeview = new TreeView(treeModel);

        // Values to be chosen in the ComboBox
        Gtk.ListStore comboModel = new ListStore(typeof(string));
        Gtk.ComboBox comboBox = new ComboBox(comboModel);
        comboBox.AppendText("<Please select>");
        comboBox.AppendText("A");
        comboBox.AppendText("B");
        comboBox.AppendText("C");
        comboBox.Active = 0;

        Gtk.TreeViewColumn comboCol = new TreeViewColumn();
        Gtk.CellRendererCombo comboCell = new CellRendererCombo();
        comboCol.Title = "Combo Column";
        comboCol.PackStart(comboCell, true);
        comboCol.AddAttribute(comboCell, "text", 1);
        comboCell.Editable = true;
        comboCell.Edited += OnEdited;
        comboCell.TextColumn = 0;
        comboCell.Text = comboBox.ActiveText;
        comboCell.Model = comboModel;
        comboCell.WidthChars = 20;

        Gtk.TreeViewColumn valueCol = new TreeViewColumn();
        Gtk.CellRendererText valueCell = new CellRendererText();
        valueCol.Title = "Value";
        valueCol.PackStart(valueCell, true);
        valueCol.AddAttribute(valueCell, "text", 1);
        valueCol.Visible = false;

        treeview.AppendColumn(comboCol);
        treeview.AppendColumn(valueCol);

        // Append the values used for the tests
        treeModel.AppendValues("comboBox1", "<Please select>"); // the string value setted for the first column does not appear.
        treeModel.AppendValues("comboBox2", "<Please select>");
        treeModel.AppendValues("comboBox3", "<Please select>");

        window.Add(treeview);
        window.ShowAll();
        Gtk.Application.Run();
    }
}
类程序
{
私有静态Gtk.TreeView TreeView=null;
静态void oneedid(对象发送方,Gtk.EditedArgs args)
{
Gtk.treeseselection selection=treeview.selection;
Gtk.TreeIter-iter;
选择。选择(退出iter);
treeview.Model.SetValue(iter,1,args.NewText);//CellRenderText
}
静态void Main(字符串[]参数)
{
Gtk.Application.Init();
Gtk.Window窗口=新窗口(“TreeView组合测试”);
window.WidthRequest=200;
window.HeightRequest=150;
Gtk.ListStore treeModel=新的ListStore(typeof(string),typeof(string));
treeview=新的treeview(treeModel);
//要在组合框中选择的值
Gtk.ListStore comboModel=新的ListStore(typeof(string));
Gtk.ComboBox ComboBox=新的ComboBox(comboModel);
comboBox.AppendText(“”);
组合框。追加文本(“A”);
组合框。追加文本(“B”);
组合框。追加文本(“C”);
comboBox.Active=0;
Gtk.TreeViewColumn comboCol=新的TreeViewColumn();
Gtk.cellRenderCombo comboCell=新的cellRenderCombo();
comboCol.Title=“组合列”;
comboCol.PackStart(comboCell,true);
comboCol.AddAttribute(comboCell,“文本”,1);
comboCell.Editable=true;
comboCell.Edited+=oneedited;
comboCell.TextColumn=0;
comboCell.Text=comboBox.ActiveText;
comboCell.Model=comboModel;
comboCell.Wi