RadContextMenu for a Grid Row

1 Answer 17 Views
ContextMenu GridView
John
Top achievements
Rank 2
Iron
Iron
Iron
John asked on 29 Jan 2025, 05:57 PM

None of the examples you guys have or I've been able to find are a solution to what should be an easy problem.  Right-click on a grid row, show the context menu, and when the user selects an items from the context menu, pass the id from the row to the page view model.

I did find this post but because it uses a UI element for the binding path and it uses code behind instead of MVVM, it's not a solution but my code is based on this.

MVVM RadGridView -> RadContextMenu -> RadMenuItem -> CommandParameter: get the row? in UI for WPF | Telerik Forums

Here is my context menu code.

<telerik:RadGridView.RowStyle>
    <Style TargetType="telerik:GridViewRow">
        <Setter Property="telerik:RadContextMenu.ContextMenu">
            <Setter.Value>
                <telerik:RadContextMenu>
                    <telerik:RadMenuItem Header="Open"
                                         Command="{Binding OpenCommand}"
                                         CommandParameter="{Binding Path=.}" />
                    <telerik:RadMenuItem Header="Delete"
                                         Command="{Binding DeleteCommand}"
                                         CommandParameter="{Binding Path=.}" />
                    <telerik:RadMenuItem Header="Print"
                                         Command="{Binding ReportCommand}"
                                         CommandParameter="{Binding Path=.}" />
                </telerik:RadContextMenu>
            </Setter.Value>
        </Setter>
    </Style>
</telerik:RadGridView.RowStyle>

The problem is it thinks the commands are in the view model for the grid row data so I'm getting binding failures. 

DeleteCommand property not found on object of type JobVm.

 

I need to have it use the commands in my page view model.  How can I achieve this?  I've tried dozens of combinations of setting the RelativeSource but none of them seem to work.

 

 

 

 

1 Answer, 1 is accepted

Sort by
0
Stenly
Telerik team
answered on 03 Feb 2025, 02:41 PM

Hello John,

The reason why the relative source is not working is that the RadContextMenu is hosted in a native WPF Popup, which is added to a different visual tree and has no direct contact with the RadGridView control.

With this in mind, to achieve this requirement, you can set the DataContext property of the RadContextMenu to be the RadGridView element via the x:Reference markup extension, as well as set the InheritDataContext property to False (since it will be set manually). This way, you should have direct contact with the command from your view model via the DataContext property of RadGridView. To pass the row, you can utilize the SelectedItem property of RadGridView, which will be updated when the RadContextMenu control is opened. This could be done via an attached property to retain a clean MVVM structure.

With this being said, I attached a sample project for you to test, which contains the implementations of the above suggestions. I hope the information and the test application will be of help to you.

Regards,
Stenly
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
ContextMenu GridView
Asked by
John
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Stenly
Telerik team
Share this question
or