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

Right-click context menu with ICommands

1 Answer 195 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Buzz
Top achievements
Rank 1
Buzz asked on 14 Dec 2010, 05:18 PM
When I right click on a row, I want the row to become selected.  I also want a context menu in which each menu item uses an ICommand.

To get the right click selection, I'm handling the context menu's Opened event, using RadContentMenu's GetClickedElement<GridViewRow>() method, and changing the gridview's SelectedItems appropriately.

The trouble is, the Command's CanExecute method is getting called before the context method's Opened is called.  So my CanExecute is analyzing the previously selected item(s).  At one point I fiddled with things enough to get the CanExecute to also be called after I changed the selection, but the user can see the menu items become enabled/disabled and that's no good.
If CanExecute is being called before the context menu opens, I need to change the selections before CanExecute and Opened are called.

How can I get the behavior I want?

Maybe if there was a PreviewOpen event on the context menu.  Or maybe I can handle a row clicked event, look for right-clicked, and change the selection there?  If so, I don't know how to do that at the moment.

Thanks for any help!
Buzz

1 Answer, 1 is accepted

Sort by
0
Buzz
Top achievements
Rank 1
answered on 14 Dec 2010, 08:19 PM
Modifying the gridview's SelectedItems when the row is right clicked seems to work for me.

<telerik:RadGridView.RowStyle>
  <Style TargetType="{x:Type telerik:GridViewRow}">
     <EventSetter Event="MouseRightButtonDown" Handler="gridViewRow_MouseRightButtonDown"/>
  </Style>
</telerik:RadGridView.RowStyle>

I am noticing that occasionally the CanExecute command method isn't getting called when the context menu pops up.  I don't know why.
Tags
GridView
Asked by
Buzz
Top achievements
Rank 1
Answers by
Buzz
Top achievements
Rank 1
Share this question
or