How to retrieve RadMenu Name from RadMenuItem?

6 posts, 0 answers
  1. Rob
    Rob avatar
    238 posts
    Member since:
    Jan 2009

    Posted 11 Feb 2009 Link to this post

    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?

     

  2. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 12 Feb 2009 Link to this post

    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.
  3. DevCraft banner
  4. Rob
    Rob avatar
    238 posts
    Member since:
    Jan 2009

    Posted 12 Feb 2009 Link to this post

    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?
  5. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 12 Feb 2009 Link to this post

    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.
  6. Preethi Gracia
    Preethi Gracia avatar
    6 posts
    Member since:
    Jul 2009

    Posted 03 Aug 2009 Link to this post

    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
  7. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 04 Aug 2009 Link to this post

    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.
Back to Top
DevCraft banner