This is a migrated thread and some comments may be shown as answers.

Override default cell context menu with autogenerated columns

1 Answer 147 Views
GridView
This is a migrated thread and some comments may be shown as answers.
ColinBlakey
Top achievements
Rank 2
ColinBlakey asked on 24 Apr 2013, 05:58 PM
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

1 Answer, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 29 Apr 2013, 12:53 PM
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.

Tags
GridView
Asked by
ColinBlakey
Top achievements
Rank 2
Answers by
Yoan
Telerik team
Share this question
or