Hi.
We're using the XamDataGrid and are trying to add a context menu. When opening the context menu we need to know the DataContext of the underlying row (the one that were right-clicked, not necessarily the selected row). If possible we'd like to create the MenuItems programmatically, as well as the MenuItem's Command.
Thanks for any help!
Lars Andreas
Hi Lars,
First you need to register the event ContextMenuOpeningEvent to every CellValuePresenter with the EventManager and create a method manipulating this event. In this metod you can make check what cell you have selected and create a different Context Menu for it. it should look something like this:
public Window1() { EventManager.RegisterClassHandler(typeof(CellValuePresenter), FrameworkElement.ContextMenuOpeningEvent, new ContextMenuEventHandler(OnCellContextMenuOpening)); InitializeComponent();
....
private static void OnCellContextMenuOpening(object sender, ContextMenuEventArgs e) { CellValuePresenter cvp = sender as CellValuePresenter; ContextMenu menu = new ContextMenu(); if(cvp.Editor is XamComboEditor) { menu = new ContextMenu(); menu.Items.Add("combo"); cvp.ContextMenu = menu; } if(cvp.Editor is XamTextEditor) { menu = new ContextMenu(); menu.Items.Add("textEditor"); cvp.ContextMenu = menu; } cvp.ContextMenu = menu; }
Hope this helps.
Or you could capture the mouserightbuttondown event and get all the info from
the datacontext (all the data for the row)
((Infragistics.Controls.Grids.RowBase)((((Infragistics.Controls.Grids.Primitives.CellControlBase)(((System.Windows.FrameworkElement)(((System.Windows.RoutedEventArgs)(e)).OriginalSource)).Parent)).Cell).Row)).Data
The row number on the screen
((((Infragistics.Controls.Grids.Primitives.CellControlBase)(((System.Windows.FrameworkElement)(((System.Windows.RoutedEventArgs)(e)).OriginalSource)).Parent)).Cell).Row).Index
the name of the column((Infragistics.Controls.Grids.ColumnBase)(((Infragistics.Controls.Grids.Primitives.CellControlBase)(((System.Windows.FrameworkElement)(((System.Windows.RoutedEventArgs)(e)).OriginalSource)).Parent)).Column)).DisplayNameResolved
The value in the column
((System.Windows.Controls.TextBlock)(((System.Windows.RoutedEventArgs)(e)).OriginalSource)).Text
Then create the menu in the codebehind.