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

How to retrieve RadMenu Name from RadMenuItem?

5 Answers 155 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Rob
Top achievements
Rank 1
Rob asked on 11 Feb 2009, 04:35 PM
I have a RadMenu filled with RadMenuItems.  On the "Click" event of the RadMenuItem, I need to be able to retrieve the name of the "RadMenu" parent...

private

void RadMenuItem_Click(object sender, RoutedEventArgs e)

 

{

 

   string MenuName = (sender as RadMenuItem)   ???.MenuName???
   ...
}

I noticed that there is a property called "Menu" which appears to be the RadMenu, but it looks like I can't access it.  Any suggestions?

 

5 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 12 Feb 2009, 07:57 AM
Hi Rob,

You can use this code to get the RadMenu (or RadContextMenu) from RadMenuItem instance:

private void OnMenuItemClick(object sender, RoutedEventArgs e)  
{  
    RadRoutedEventArgs args = e as RadRoutedEventArgs;  
    RadMenuItem menuItem = args.OriginalSource as RadMenuItem;  
 
    ItemsControl parent = ItemsControl.ItemsControlFromItemContainer(menuItem);  
    RadMenu menu = parent as RadMenu;  
 
    while (parent != null)  
    {  
        if (menu != null)  
        {  
            break;  
        }  
        parent = ItemsControl.ItemsControlFromItemContainer(parent);  
        menu = parent as RadMenu;  
    }  
 
    if (menu != null)  
    {  
        Debug.WriteLine("Menu Name is " + menu.Name);  
    }  

Let me know if you need more help.

All the best,
Hristo
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Rob
Top achievements
Rank 1
answered on 12 Feb 2009, 02:23 PM
Great.  Thanks.  I was hoping there was a property but had a sneaky suspicion that recursively going back up the tree was the way.  We're going to add this as an extension method so we can use it throughout our app.  Any chance you'll be enabling this property in the future?
0
Hristo
Telerik team
answered on 12 Feb 2009, 05:57 PM
Hi Rob,

Not sure about exposing this in the future. Maybe if there is enough demand.


Regards,
Hristo
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
Preethi Gracia
Top achievements
Rank 1
answered on 03 Aug 2009, 11:12 AM
Hello,
    I have a different scenario. I have a list of Menus which are dynamically generated. The menuItems have muliple levels of menu items. I have successfully written code to generate the base level menus. Now I need to acess the specific MenuItem objects at Runtime to add the next level of Menus. The entire list of Menus all known only at Runtine time. Please help with me some sample code. I am using RadMenu of RadControls for Silverlight Q2 2009.

Thanks
Preethi
0
Hristo
Telerik team
answered on 04 Aug 2009, 08:02 AM
Hi Preethi Gracia,

The best way to do this is to create your own collection that support CollectionChanged notification (like ObservableCollection) and add items in it. This items can be collection and they will represent MenuItem with sub items. You should set HierarchicalDataTemplate on the RadMenu to make hierarchy.
We have such example here:
http://demos.telerik.com/silverlight/#Menu/DataBinding

Let us know if you need more information.

Kind regards,
Hristo
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.
Tags
Menu
Asked by
Rob
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Rob
Top achievements
Rank 1
Preethi Gracia
Top achievements
Rank 1
Share this question
or