Question

Could somebody point me in the right direction on how to update a Gtk.TreeView after changing a CellRendererCombo in Gtk#?

Since the only example I found was in Python, I tried to port the example to C#, but without success so far.

The Python example is here: http://learngtk.org/pygtk-tutorial/cellrenderercombo.html

In the code below I am having difficulties with the method ComboChanged.

After changing the value in the combobox (by selecting a different value) and placing the focus outside of the combobox, the value does not change.

using System;
using Gtk;
using System.Collections.Generic;

public partial class MainWindow: Gtk.Window
{   
    public MainWindow (): base (Gtk.WindowType.Toplevel)
    {
        Build ();
        var tvComboBox = InitTreeViewWithComboBox ();
        var vbox = new Gtk.VBox ();
        vbox.PackStart (tvComboBox, true, true, 0);
        this.Add (vbox);
        this.ShowAll ();
    }       

    // adopted from http://learngtk.org/pygtk-tutorial/cellrenderercombo.html
    ListStore liststore_hardware;
    ListStore liststore_manufacturers;

    private TreeView InitTreeViewWithComboBox ()
    {
        liststore_manufacturers = new Gtk.ListStore(typeof (string));
        var manufacturers = new List<string> {"Sony", "LG", "Panasonic", "Toshiba", "Nokia", "Samsung"};
            foreach (var item in manufacturers) {
            liststore_manufacturers.AppendValues (item);
        }

        liststore_hardware = new Gtk.ListStore(typeof (string), typeof (string));
        liststore_hardware.AppendValues ("Television", "Samsung");
        liststore_hardware.AppendValues ("Mobile Phone", "LG");
        liststore_hardware.AppendValues ("DVD Player", "Sony");

        var treeview = new Gtk.TreeView ();
        treeview.Model = liststore_hardware;

        var column_text = new TreeViewColumn { Title = "Text" };
        var column_combo = new TreeViewColumn { Title = "Combo" };
        treeview.AppendColumn (column_text);
        treeview.AppendColumn (column_combo);

        var cellrenderer_text = new CellRendererText ();
        column_text.PackStart (cellrenderer_text, false);
        column_text.AddAttribute (cellrenderer_text, "text", 0);


        var cellrenderer_combo = new CellRendererCombo ();
        cellrenderer_combo.Editable = true;
        cellrenderer_combo.Model = liststore_manufacturers;
        cellrenderer_combo.TextColumn = 0;
        column_combo.PackStart (cellrenderer_combo, false);
        column_combo.AddAttribute (cellrenderer_combo, "text", 1);

        cellrenderer_combo.Edited += ComboChanged;

        return treeview;
    }

    void ComboChanged (object o, EditedArgs args)
    {
        // Not really sure what to do here....

        /*
        var crc = o as CellRendererCombo;
        TreeIter iter; // index within the combobox
        if (!crc.Model.GetIterFirst (out iter)) {
            return;
        }
        crc.Model.SetValue (iter, 0, args.NewText);
        liststore_hardware.SetValue (iterHardware, 1, args.NewText);
        */
    }
Was it helpful?

Solution

Going through the API a bit more I found the solution...:

void ComboChanged (object o, EditedArgs args)
{
    TreeSelection selection = treeview.Selection;
    TreeIter iter;
    if (!selection.GetSelected (out iter)) {
        return;
    }
    liststore_hardware.SetValue (iter, 1, args.NewText);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top