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

RadGridView + RadContextMenu: pass row DataContext as CommandParameter

1 Answer 419 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Remco
Top achievements
Rank 1
Remco asked on 12 Mar 2012, 06:00 PM
Hello,

I am trying to use a RadContextMenu with a RadGridView. When I right click on a row I want a context menu to appear with a menu item whose Command is databound to an ICommand on my viewmodel and that passes in the row's DataContext as the CommandParameter. In Telerik's documentation I see Telerik set the RadGridView.SelectedItem to the row's DataContext in a RadContextMenu.Opened event handler. But I do not want to set the RadGridView.SelectedItem (if I did, then I would prefer to set the ICollectionView.CurrentItem in the viewmodel using the CommandParameter value). I suppose I could set the CommandParameter on the menu item in a RadContextMenu.Opened event handler, but I would prefer to set the CommandParameter using databinding. However, I can only get to the row by calling GridContextMenu.GetClickedElement<GridViewRow>(), so I cannot use the standard Binding markup extension. I wonder if a custom markup extension could do the trick. Has anybody done something like that or is there a better / easier way?

cheers

Remco

1 Answer, 1 is accepted

Sort by
1
Remco
Top achievements
Rank 1
answered on 13 Mar 2012, 09:34 AM
never mind, I managed to get the row's DataContext passed into the CommandParameter using a behavior:
public class SetRowDataContextCommandParameterBehavior : Behavior<RadMenuItem>
{
    protected override void OnAttached()
    {
        base.OnAttached();
 
        this.AssociatedObject.Loaded += this.OnMenuItemLoaded;
    }
 
    protected override void OnDetaching()
    {
        base.OnDetaching();
 
        this.AssociatedObject.Loaded -= this.OnMenuItemLoaded;
    }
 
    private void OnMenuItemLoaded(object sender, RoutedEventArgs e)
    {
        var menu = this.AssociatedObject.Menu as RadContextMenu;
        if (menu != null)
        {
            var row = menu.GetClickedElement<GridViewRow>();
            if (row != null)
            {
                this.AssociatedObject.CommandParameter = row.DataContext;
            }
            else
            {
                menu.IsOpen = false;
            }
        }
    }
}

used as follows inside the grid:

<telerik:RadContextMenu.ContextMenu>
                    <telerik:RadContextMenu>
                        <telerik:RadMenuItem Header="{Binding OpenEntryCommand.Text}" Command="{Binding OpenEntryCommand}" >
                            <i:Interaction.Behaviors>
                                <Behaviors:SetRowDataContextCommandParameterBehavior />
                            </i:Interaction.Behaviors>
                        </telerik:RadMenuItem>
                    </telerik:RadContextMenu>
                </telerik:RadContextMenu.ContextMenu>

Tags
GridView
Asked by
Remco
Top achievements
Rank 1
Answers by
Remco
Top achievements
Rank 1
Share this question
or