我有一个经过过滤然后排序的 ListStore。它看起来像这样:
// Create a model for the cards
cardListStore = new ListStore (typeof (Card));
// Set up the tree view
cardFilter = new TreeModelFilter (cardListStore, null);
cardFilter.VisibleFunc = new TreeModelFilterVisibleFunc (FilterCards);
cardSort = new TreeModelSort (cardFilter);
cardTreeView.Model = cardSort;
当我右键单击每一行时,我想获得一个特定于每一行的上下文菜单。我的点击处理程序看起来像这样:
[GLib.ConnectBeforeAttribute]
void HandleCardTreeViewButtonPressEvent (object o, ButtonPressEventArgs args)
{
if (args.Event.Button != 3)
return;
TreePath path;
// If right click on empty space
if (!cardTreeView.GetPathAtPos (Convert.ToInt32 (args.Event.X),
Convert.ToInt32 (args.Event.Y),
out path)) {
MakeCardEmptySpaceContextMenu ().Popup ();
return;
}
TreeIter iter;
if (!cardListStore.GetIter (out iter, path))
return;
Card card = (Card) cardListStore.GetValue (iter, 0);
MakeCardContextMenu (card, iter).Popup ();
}
这在 ListStore 未被过滤或排序时起作用。但如果是,它会给出错误的行。
例如,假设行在排序之前看起来像这样:
一个
乙
C
排序后,它们看起来像这样:
乙
一个
C
右键单击第二行(“A”)将为您提供“B”,因为这是模型排序之前 B 所在的位置。过滤也会发生同样的事情。假设模型经过过滤后如下所示:
一个
C
右键单击第二行(“C”)仍会显示“B”。
知道如何解决这个问题吗?
最佳答案
我只需要从 cardSort 而不是 cardListStore 获取 iter 和值。
if (!cardListStore.GetIter (out iter, path))
return;
Card card = (Card) cardListStore.GetValue (iter, 0);
成为
if (!cardSort.GetIter (out iter, path))
return;
Card card = (Card) cardSort.GetValue (iter, 0);
关于c# - 在 Gtk 中,如何从按 x 和 y 坐标排序的 TreeView 中获取路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2233545/