tags:

views:

13

answers:

1

I'm trying to display a context menu where the user clicks the mouse in a WPF application. I've handled the OpenContextMenu event and the handler has a pair of doubles, e.CursorLeft and e.CursorTop which are the coordinates of the mouse click relative to the control that was clicked (in this case a DataGridCell). If I display the context menu using those coordinates it appears relative to the application window offset by the cursor amounts.

How can I convert those DataGridCell-relative Cursor coordinates into window coordinate space?

+2  A: 

You can use the UIElement.TranslatePoint(Point, Visual) method to convert coordinates from one control's coordinate space to another. The following code should do what you want (not tested!):

Point target =
    myDataGridCell.TranslatePoint(new Point(e.CursorLeft, e.CursorTop), Application.Current.MainWindow);

However, if you only want to display a context menu, you could also simply assign the FrameworkElement.ContextMenu property for the control which should show the context menu. This way, the position will be positioned at the mouse cursor automatically. If you have a more complicated scenario, you might still use the method above.

gehho