1

I want to simulate a right click on datagrid in WPF, and a context menu pop up. But I found that the context menu is popped up via the PopupControlService which is internal to MS and I cannot access. Now I have the datagrid instance. How can I simulate a right click on this datagrid? Thank you all.

Peng Deng
  • 43
  • 8

2 Answers2

1
DataGrid.RaiseEvent(new RoutedEventArgs(ContextMenuOpeningEvent));

Have you tried it?

Or

 DataGrid.RaiseEvent(new RoutedEventArgs(MouseRightButtonDownEvent));

Look here: https://timscyclingblog.wordpress.com/2012/04/05/wpf-simulate-mouse-click-in-code-behind/

And also here: Raising WPF MouseLeftButtonDownEvent event

Iaroslav
  • 295
  • 1
  • 10
  • 1
    First of all, thank you for your help. But I had tried these two ways. Both didn't work. For the first one it complained that "System.ArgumentException : Object of type 'System.Windows.RoutedEventArgs' cannot be converted to type 'System.Windows.Controls.ContextMenuEventArgs'." And the second one: "System.ArgumentException : Object of type 'System.Windows.RoutedEventArgs' cannot be converted to type 'System.Windows.Input.MouseButtonEventArgs'" – Peng Deng Jan 04 '18 at 01:24
0

To by-passe the problem you can create manually the context menu and show it in the code behind ?