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
1415
ContextMenu with XamDataGrid
posted

Is there a way to enable/disable the MenuItem of a ContextMenu depending on a value of the row being selected/right-clicked?

  • 18204
    Verified Answer
    Offline posted

    Hello PMac,

    Thank you for posting in our forums!

    You can handle the XamDataGrid's ContextMenuOpening event and edit the MenuItems from there.

    Below, you can see a C# code snippet of how to achieve this.

    private void XamDataGrid_ContextMenuOpening(object sender, ContextMenuEventArgs e)
    {
        DataRecordPresenter drp = Utilities.GetAncestorFromType((DependencyObject)e.OriginalSource, typeof(DataRecordPresenter), true) as DataRecordPresenter;
        DataRecord dr = drp.Record as DataRecord;
        Cell cell = dr.Cells["Name"];
    
        if (cell != null)
        {
            XamDataGrid xdg = sender as XamDataGrid;
            MenuItem deleteMenuItem = xdg.ContextMenu.Items[2] as MenuItem;
            deleteMenuItem.IsEnabled = ("Computer" != cell.Value.ToString());
        }
    }

    If you have any further questions or concerns with this, please let me know and I will be glad to help.


    Sincerely,
    Michael H.
    Software Developer
    Infragistics, Inc.
    www.infragistics.com/support