How to knwo the contexmenu is getting opened.

2 posts, 0 answers
  1. Dharmavaram
    Dharmavaram avatar
    102 posts
    Member since:
    Jun 2013

    Posted 20 Jul 2017 Link to this post

    I want to know how the context menu is being opened in "Opening" event of it.

    Usually there are two ways to open it, One is by right clicking other is by Hitting on key.Apps' on keyboard.

    This need is because based on way of opening the context menu changes my menu items. 


    <telerik:RadTreeListView x:Name="RadTreeListView1"                          IsSynchronizedWithCurrentItem="True"                         IsReadOnly="True"                         ItemsSource="{Binding Folders}"                         Width="600"                                                                        MinHeight="300" SelectedItem="{Binding SelectedItem}"                         Height="550"                         CanUserFreezeColumns="False"                         RowIndicatorVisibility="Collapsed"                          SelectionMode="Extended"                          ColumnWidth="*" local:MySelectedItemsBindingBehavior.SelectedItems="{Binding SelectedItemCollection}">             <telerik:EventToCommandBehavior.EventBindings>                 <telerik:EventBinding EventName="SelectionChanging"                                Command="{Binding SelectionChangedCommand}"                                PassEventArgsToCommand="True"/>                 <telerik:EventBinding EventName="MouseRightButtonDown" Command="{Binding testingtesting}"                               PassEventArgsToCommand="True"/>                 <telerik:EventBinding EventName="KeyDown" Command="{Binding KeyDownCmd}"                               PassEventArgsToCommand="True"/>             </telerik:EventToCommandBehavior.EventBindings>             <telerik:RadContextMenu.ContextMenu >                 <telerik:RadContextMenu x:Name="GridContextMenu" ItemsSource="{Binding ContextMenuSelectedItems}">                     <telerik:EventToCommandBehavior.EventBindings>                         <telerik:EventBinding EventName="Opening"                                Command="{Binding ContextButtonClick}"                                PassEventArgsToCommand="True"/>                     </telerik:EventToCommandBehavior.EventBindings>                 </telerik:RadContextMenu>             </telerik:RadContextMenu.ContextMenu>         </telerik:RadTreeListView>


    Event args for my "ContextButtonClick" command does not have any information about how it is being opened.

    Having validation check of "KeyBoard.IsDown(Key.Apps) or KeyBoard.IsUp(Key.Apps)" is also not solving because, after releasing the "apps" button only context menu will start opening, so at point checking the stat of "Apps" button doesn't give us confident results.



    Any help in this regards is much appreciated. 



  2. Martin Ivanov
    Martin Ivanov avatar
    2244 posts

    Posted 25 Jul 2017 Link to this post

    Hello Dharmavaram,

    There is no straightforward approaches to achieve the desired effect. However, I can suggest you couple of options which you can try.
    • The first one is to use the MousePosition property of RadContextMenu. It returns the mouse position at the moment when the menu opens. If the menu is not opened via the mouse, the position will be Point(NaN, NaN). So, if you need only to determine if the mouse has opened the menu or not, this is the way to go.
      private void RadContextMenu_Opening(object sender, Telerik.Windows.RadRoutedEventArgs e)
          var menu = (RadContextMenu)sender;
          if (menu.MousePosition.Equals(new Point(double.NaN, double.NaN)))
              // Then the menu was opened using an approach different than the mouse.
              // Note that you will hit this code block in all other cases. When the menu is opened via keyboard or manually opened.
    • Another approach is to subscribe for the KeyDown event of the element which is parent of the context menu (RadTreeListView in your case) and save the pressed key if is the one that should open the menu. 
      private List<Key?> openContextMenuAvailableKeys = new List<Key?>() { Key.Apps };
      private Key? pressedKey;
      private void RadListBox_KeyDown(object sender, KeyEventArgs e)
          if (openContextMenuAvailableKeys.Contains(this.pressedKey))
              this.pressedKey = e.Key;
      private void GridContextMenu_Opening(object sender, Telerik.Windows.RadRoutedEventArgs e)
          if (this.pressedKey != null && openContextMenuAvailableKeys.Contains(this.pressedKey))
              // Do whatever you do with the key
          this.pressedKey = null;           

    I hope this helps.

    Martin Ivanov
    Progress Telerik
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Back to Top