سؤال

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?

هل كانت مفيدة؟

المحلول

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.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top