GTK CellRendererCombo将值设置为适当的单元格

时间:2012-12-05 01:35:31

标签: combobox gtk treecellrenderer

我正在尝试使用具有多个列的TreeView。第一列是ComboBox,这意味着我使用Gtk.CellRendererCombo。问题是,当我从ComboBox中选择一个值时,我希望单元格中的Text从“”更改为我刚刚选择的值。如果在Gtk.CellRendererCombo.Edited事件中我将列Text字段设置为EditedArgs.NewText,那是可行的。

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

Gtk.CellRenderer不应该包含任何状态,所以好吧,使用Text字段从我正在尝试做的事情来看是一个非常糟糕的主意。

但是如果我从Gtk.ListStore设置了一些值,它是我的TreeView的模型(与Gtk.CellRendererCombo的模型不同)。设置的值永远不会显示在ComboBox的列中。

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();
    }
}

我想要选择ComboBox的Cell来显示值,但继续可以编辑以供以后更改。

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

更新

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

1 个答案:

答案 0 :(得分:0)

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

结果代码如下:

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();
    }
}