检测单元格单击Mono TreeView

时间:2016-05-07 10:47:43

标签: c# mono treeview monodevelop gtk#

我的Mono应用程序中有一个树形视图,我正在使用GTK#。我希望能够检测用户点击了哪个Tree View单元格并从该单元格中获取数据,但在Mono文档中找不到任何有用的内容。

是否有人为此或任何建议找到了解决方案?

非常感谢

1 个答案:

答案 0 :(得分:0)

我通过在树视图中保存由树路径键入的对象字典来完成此操作。例如。类似的东西:

IDictionary<string, MyType> thePathKeyedObjects = new Dictionary<string, MyType>();

Gtk.TreeStore _ts = new TreeStore (typeof(string));
foreach (MyType _t in _somelistofmytypes) {
    Gtk.TreeIter _it = _ts.AppendValues (_t.SomeTextForTheTreeView);
    thePathKeyedObjects.Add(_ts.GetPath (_it).ToString (), _t);
}
treeview.Model = _ts;

// and then later...

Gtk.TreeIter _it;
Gtk.TreeModel _mdl;
if (treeview.Selection.CountSelectedRows () > 0) {
    treeview.Selection.GetSelected (out _mdl, out _it);
    string _path = _mdl.GetPath (_it).ToString ();
    MyType _selected_t = thePathKeyedObjects[_path];
}