0

How to make a NodeView cell retain the entered value after text's been entered through keyboard and the Edited event fired?

Whenever I enter some text into the cell and try to confirm that change, the old value that was there before my editing comes back.

The property of the subclass that should hold the value of a node never gets updated with the new value.

How do I get the text entered into a NodeView cell in the first place?

Luka
  • 1,718
  • 3
  • 19
  • 29

1 Answers1

2

The trick is to use the Path property of the Gtk.EditedArgs argument passed to your event handler to get the correct node from the store and modify (you're responsible to propagate the change from the UI to your model). A small, complete example follows.

Given the following Gtk.TreeNode implementation:

[Gtk.TreeNode]
public class MyTreeNode : Gtk.TreeNode 
{        
    public MyTreeNode(string text)
    {
        Text = text;
    }

    [Gtk.TreeNodeValue(Column=0)]
    public string Text;
}

it is easy to change the Text property as follows:

Gtk.NodeStore store = new Gtk.NodeStore(typeof(MyTreeNode));
store.AddNode(new MyTreeNode("The Beatles"));
store.AddNode(new MyTreeNode("Peter Gabriel"));
store.AddNode(new MyTreeNode("U2"));

Gtk.CellRendererText editableCell = new Gtk.CellRendererText();

Gtk.NodeView view = new Gtk.NodeView(store);
view.AppendColumn ("Artist", editableCell, "text", 0);
view.ShowAll();

editableCell.Editable = true;
editableCell.Edited += (object o, Gtk.EditedArgs args) => {
    var node = store.GetNode(new Gtk.TreePath(args.Path)) as MyTreeNode;
    node.Text = args.NewText;
};

Note:

  1. the use of args.Path to get the correct MyTreeNode from the store; and
  2. the cast of the result to MyTreeNode to be able to access the Text property.
fog
  • 3,266
  • 1
  • 25
  • 31
  • how to bind data to NodeView in GTK sharp. – Anjali Sep 16 '16 at 07:16
  • Is there a way to do exactly what you do in your answer, but edit another column? `args` provides the path of the edited cell, but that path only contains the row, and not the column. – zdimension Oct 11 '16 at 17:53