MVVM RadGridView -> RadContextMenu -> RadMenuItem -> CommandParameter: get the row?

1 Answer 184 Views
ContextMenu GridView
Brad
Top achievements
Rank 1
Brad asked on 28 Mar 2023, 07:50 PM

Today I tried to put a context menu on a row in a RadGridView that would operate on the row clicked.  I was surprised to find it was not nearly as trivial as I expected.  All the documentation on this is either very old or accomplishing it via code or code-behind.  The "official" demo even has an entry for "Row Context Menu", which sounds tantalizingly close until one looks at the code.  It's certainly not MVVM!

This should be as simple as the following:

<telerik:RadGridView ItemsSource="{Binding Path=MyRecords}">
    <telerik:RadContextMenu.ContextMenu>
        <telerik:RadContextMenu >
            <telerik:RadMenuItem Header="Do something to this record" Command="{Binding MyRowCommand}" CommandParameter="{Binding ?????}" />
        </telerik:RadContextMenu>
    </telerik:RadContextMenu.ContextMenu>
	...
</telerik:RadGridView>

The problem is that I can't for the life of me figure out what to pass as the command parameter that will help me identify the row.

Any help would be much appreciated!

1 Answer, 1 is accepted

Sort by
1
Accepted
Martin Ivanov
Telerik team
answered on 29 Mar 2023, 09:12 AM

Hello Brad,

The "Row Context Menu" demo shows how to implement a custom behavior that adds, removes and deletes records in a generic way. The behavior doesn't depend on the view model, but only on the Items collection of RadGridView, which is synchronized to whatever IEnumerable is set to the ItemsSource. The idea is to be able to use it any data items you have in the view model. The solution is not MVVM, but it doesn't break the pattern and also its idea is not to be MVVM, since this will couple it with the view model in this concrete example.

About your requirement, the code snippet shows that the ContextMenu is assigned to the RadGridView. This is convenient if you use a behavior (like in the demo) or code-behind, but this setup won't allow you to get the row under the mouse. To do that, you should select the right clicked row before that and then use the SelectedItem of RadGridView, and if you have multiple selection enabled it becomes even more code-behind dependent solution. 

What I can recommend you instead of that is to define the menu on the GridViewRow controls. This way you can use the data context of the row as the CommandParameter. To assign the menu to the row, you can use the RowStyle property of RadGridView. I've attached a small sample showing this approach.

I hope that helps.

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Brad
Top achievements
Rank 1
commented on 29 Mar 2023, 04:03 PM

The attached sample solved my problem perfectly.  Thanks a lot!
Tags
ContextMenu GridView
Asked by
Brad
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or