RadContextMenu dynamically enable/disable based on DataGrid Cell

6 posts, 0 answers
  1. John
    John avatar
    6 posts
    Member since:
    Nov 2008

    Posted 04 Aug 2009 Link to this post

    Hi

    I've been trying to put together a simple example of how best to utilise context menu control with the standard silverlight datagrid.

    I want to enable/disable menu items based upon the row and column the user right clicked over.  The obvious place to do this appears to be in the menu.opened event, but I cannot see a way for me to determine the row/column at that point in time.

    Has anyone any experience/comments

    Thanks
  2. Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 05 Aug 2009 Link to this post

    Hello John,

    I answered your support ticket, but in case anyone else has the same problem I am pasting the text here:

    RadContextMenu has a method, called GetClickedElement<T>() that will return the topmost element of the specified type at the coordinates of the mouse when the menu was opened. So I guess the following code should be of help:
    DataGridRow row = (sender as RadContextMenu).GetClickedElement<DataGridRow>();


    Greetings,
    Valeri Hristov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. DevCraft banner
  4. Naseem
    Naseem avatar
    70 posts
    Member since:
    Nov 2010

    Posted 30 Nov 2010 Link to this post

    Hi,

    I have got the issue that John mentioned . I need to show the context menu on just some of the columns (not all of them) , for that purpose I need to get the CurrentColumn.
    the GetClickedElement gave me the current selected row ,but I couldn't find anything for getting current column or current cell. 
    DataGridRow row = (sender as RadContextMenu).GetClickedElement<DataGridRow>();

    I appreciate your help,

    Thank you,
  5. Naseem
    Naseem avatar
    70 posts
    Member since:
    Nov 2010

    Posted 30 Nov 2010 Link to this post

    Thank you ,

    I have realized what should I do,

     GridView.GridViewCell cell= MarketContextMenu.GetClickedElement<GridView.GridViewCell>();

     

     

    cell.Column give me the selectedColumn :)

  6. Ryan
    Ryan avatar
    18 posts
    Member since:
    Oct 2010

    Posted 05 Jan 2011 Link to this post

    Once you've retrieved the cell and row and determined that you're on a cell that you don't want to display the menu, how do you close it? I'm currently setting the visibility to Collapsed, which doesn't show it, but the menu is still open. This means that the next click closes the hidden menu instead of opening a new one, or whatever other action the user wants to perform.
  7. Naseem
    Naseem avatar
    70 posts
    Member since:
    Nov 2010

    Posted 05 Jan 2011 Link to this post

    Hi Ryan,

    In my case the menu is static and I don't need to renew it each time it is opened. I just needed not to show the menu in some of the specific columns , so the Visibility property is working for me. The bellow code is what I have done.

    Hope it helps.

    <GridView:RadGridView Grid.Row="2" x:Name="dgrdProduct" AlternationCount="2" SelectedCellsChanged="dgrdProduct_SelectedCellsChanged" Telerik:StyleManager.Theme="Office_Blue" ShowGroupPanel="False" RowIndicatorVisibility="Collapsed" AutoGenerateColumns="false" SelectionMode="Extended" >
                    <telerik:RadContextMenu.ContextMenu>
      
                            <telerik:RadContextMenu x:Name="ProductContextMenu" Opened="RadContextMenu_Opened" ItemClick="RadContextMenu_ItemClick">
                                <telerik:RadContextMenu.Items>
                                    <telerik:RadMenuItem Header="Edit" >
                                        <telerik:RadMenuItem Header="Edit Selected"/>
                                        <telerik:RadMenuItem Header="Edit All"/>
                                        <telerik:RadMenuItem Header="Clear Selected"/>
                                        <telerik:RadMenuItem Header="Clear All"/>
                                    </telerik:RadMenuItem>
                                    <telerik:RadMenuItem Header="Selection" >
                                        <telerik:RadMenuItem Header="SelectAll"/>
                                        <telerik:RadMenuItem Header="UnSelectAll"/>
                                    </telerik:RadMenuItem>
                                </telerik:RadContextMenu.Items>
                            </telerik:RadContextMenu>

                        </telerik:RadContextMenu.ContextMenu>
      
                    </GridView:RadGridView>
    private void RadContextMenu_Opened(object sender, RoutedEventArgs e)
           {
                currentCell = ProductContextMenu.GetClickedElement<Telerik.Windows.Controls.GridView.GridViewCell>();

               if (currentCell != null)
               {
                   currentFilterInfo = lstFilterInfo.Where(x => x.FilterDesc == currentCell.Column.Header.ToString()).FirstOrDefault();

                  Telerik.Windows.Controls.GridView.GridViewRow currentrow = ProductContextMenu.GetClickedElement<Telerik.Windows.Controls.GridView.GridViewRow>();

                   if (currentFilterInfo == null)
                   {
                       ProductContextMenu.Visibility = Visibility.Collapsed;
                   }
                   else
                   {
                       ProductContextMenu.Visibility = Visibility.Visible;
                   }
               }
               else
               {
                   ProductContextMenu.Visibility = Visibility.Collapsed;
               }
           }

Back to Top
DevCraft banner