Right Click Context Menu/Select on GridView

2 posts, 0 answers
  1. heavywoody
    heavywoody avatar
    85 posts
    Member since:
    Oct 2009

    Posted 10 May 2014 Link to this post

     I am running into a selection issue with WPF GridView.   I need the Grid to work just like Windows Explorer as far as using a Context Menu on right click.  Meaning if the user right clicks an item or range of items, the context menu will open and those items will be selected.  What is throwing me if that if I try to put in code to right click/select, I can't get the other items like multiple select using CTRL or SHIFT to work correctly.  So just like Windows Explorer, the rules on selecting would be:

    1) User can right click and select an item without left clicking to select first.
    2) User can hold down SHIFT and select a range of items then right click/context menu on selected items.
    3) User can hold down CTRL and select individual items then right click/context menu on those selected items.

    This is what I have tried but it doesn't work if I am using CTRL, selecting items, then right clicking to get Context Menu.  So do you have any suggestions or samples to make it work EXACTLY like Windows Exploring for selecting items?

     private void GroupGrid_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)

                var s = e.OriginalSource as FrameworkElement;
                var parentRow = s.ParentOfType<GridViewRow>();
                var rowItem = parentRow as GridViewRowItem;
                var mediaItem = rowItem as RadRowItem;
                if (gridAdvancedSearch.SelectedItems.Count > 1 && gridAdvancedSearch.SelectedItems.Contains(mediaItem.Item))
                    e.Handled = true;

                    if (parentRow != null)
                        parentRow.IsSelected = true;


  2. Dimitrina
    Dimitrina avatar
    3769 posts

    Posted 12 May 2014 Link to this post


    Generally you can find out what the modifier key pressed is through the Keyboard.Modifiers. 
    The code would be similar to:
    var controlPressed = Keyboard.Modifiers == ModifierKeys.Control; 

    You can also check the "Row Context Menu" WPF Demo.

    How does this work for you?

    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
Back to Top