Override default cell context menu with autogenerated columns

2 posts, 0 answers
  1. ColinBlakey
    ColinBlakey avatar
    21 posts
    Member since:
    Aug 2006

    Posted 24 Apr 2013 Link to this post

    I've searched for a while but cannot seem to find a clear answer that works for the issue. I have a datagrid that is entirely auto generated. To this I added a context menu for right clicks when the cell is not in editable mode (see below). I would like to use the same menu when the cell is in edit mode i.e. I want to override the default context menu. I've tried various options including adding a CellEditTemplate in the code behind but this does not work because I need to reference the instance of the context menu in a non static (not sealed) context and templates only appear to want to work in a static/sealed context. What should I be doing?

    <telerik:RadGridView x:Name="dataGridView" Margin="5,10,5,5"
        AutoGenerateColumns="True"
        AutoGeneratingColumn="DataGridView_AutoGeneratingColumn"
        ...other options....>
        <telerik:RadGridView.ContextMenu>
            <ContextMenu x:Name="gridContextMenu">
                <MenuItem x:Name="clearCellMenuItem" Header="Clear Cell" />
                <MenuItem x:Name="clearRowMenuItem" Header="Clear Row" />
                <MenuItem x:Name="clearAllMenuItem" Header="Clear All" />
                <Separator x:Name="separatorMenuItem" />
                <MenuItem x:Name="copyMenuItem" Command="Copy" Header="_Copy" />
                <MenuItem x:Name="pasteMenuItem" Command="Paste" Header="_Paste" />
            </ContextMenu>
        </telerik:RadGridView.ContextMenu>
    </telerik:RadGridView>

    Thanks

    Colin
  2. Yoan
    Admin
    Yoan avatar
    1189 posts

    Posted 29 Apr 2013 Link to this post

    Hello Colin,

    You can handle the PreviewMouseRightButtonDown event and show your ContextMenu like so:

    this.AddHandler(FrameworkElement.PreviewMouseRightButtonDownEvent, new RoutedEventHandler(OnMouseRightButton), true);
             
            private void OnMouseRightButton(object sender, RoutedEventArgs e)
            {
                RadContextMenu contextMenu = new RadContextMenu();
                contextMenu.Items.Add(new RadMenuItem() { Header = "test item" });
                contextMenu.PlacementTarget = this.myRadGridView1;          
                contextMenu.IsOpen = true;
            }

    Hopefully this helps!

    Regards,
    Yoan
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top