[Gtk-sharp-list] Problem getting a TreePath from a TreeModelSort by x and y coords

Matthew Pirocchi matthew.pirocchi at gmail.com
Tue Feb 9 19:24:45 EST 2010

I have a ListStore that is filtered and then sorted. It looks
something like this:

// 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;

I want to have a get a context menu specific for each row when I
right-click on it. My click handler looks something like this:

void HandleCardTreeViewButtonPressEvent (object o, ButtonPressEventArgs args)
 if (args.Event.Button != 3)

 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 ();

 TreeIter iter;
 if (!cardListStore.GetIter (out iter, path))

 Card card = (Card) cardListStore.GetValue (iter, 0);

 MakeCardContextMenu (card, iter).Popup ();

This works when the ListStore is not filtered or sorted. But when it
is, it gives the wrong row.

For example, say the rows look like this before they are sorted:


And after they are sorted, they look like this:


Right-clicking on the second row ("A") will give you "B", because
that's where B was before the model was sorted. The same thing happens
for filtering. Say the model, after it is filtered, looks like this:


Right-clicking on the second row ("C") would still give you "B".

Any idea how to work around this?

More information about the Gtk-sharp-list mailing list