Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
60
Find the right-clicked row that caused a ContextMenu to open
posted

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

Parents
  • 69686
    posted

     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. 

Reply Children