Right-click context menu with ICommands

2 posts, 0 answers
  1. Buzz
    Buzz avatar
    32 posts
    Member since:
    Oct 2010

    Posted 14 Dec 2010 Link to this post

    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
  2. Buzz
    Buzz avatar
    32 posts
    Member since:
    Oct 2010

    Posted 14 Dec 2010 Link to this post

    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.
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top