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

RadContextMenu dynamically enable/disable based on DataGrid Cell

5 Answers 486 Views
Menu
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 04 Aug 2009, 10:55 AM
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

5 Answers, 1 is accepted

Sort by
0
Valeri Hristov
Telerik team
answered on 05 Aug 2009, 01:46 PM
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.
0
Naseem
Top achievements
Rank 1
answered on 01 Dec 2010, 12:46 AM
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,
0
Naseem
Top achievements
Rank 1
answered on 01 Dec 2010, 04:07 AM
Thank you ,

I have realized what should I do,

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

 

 

cell.Column give me the selectedColumn :)

0
Ryan
Top achievements
Rank 1
answered on 05 Jan 2011, 04:42 PM
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.
0
Naseem
Top achievements
Rank 1
answered on 05 Jan 2011, 11:03 PM
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;
           }
       }

Tags
Menu
Asked by
John
Top achievements
Rank 1
Answers by
Valeri Hristov
Telerik team
Naseem
Top achievements
Rank 1
Ryan
Top achievements
Rank 1
Share this question
or