RadGridView + RadContextMenu: pass row DataContext as CommandParameter

2 posts, 0 answers
  1. Remco
    Remco avatar
    13 posts
    Member since:
    Jan 2012

    Posted 12 Mar 2012 Link to this post

    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
  2. Remco
    Remco avatar
    13 posts
    Member since:
    Jan 2012

    Posted 13 Mar 2012 Link to this post

    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>

Back to Top