RadMenu with a dynamically populated submenu; need to access submenu's RadMenuItems

2 posts, 0 answers
  1. Jon
    Jon avatar
    3 posts
    Member since:
    May 2011

    Posted 13 Feb 2012 Link to this post


    I currently have a RadMenu with a static top-level RadMenuItem, which in turn contains a dynamically-populated submenu. What I'm trying to do is change each dynamic RadMenuItem's Foreground brush at runtime, but I'm not able to access the submenu's RadMenuItems until the user has hovered over the static RadMenuItem, causing the submenu to be drawn.

    Here is the structure:
    * RadMenu
      * RadMenuItem (static; Header = "Products"; ItemsSource = bound to a collection of business entities)
        * dynamic submenu
          * RadMenuItem (dynamic; Header = "Product 1 Name")
          * RadMenuItem (dynamic; Header = "Product 2 Name")
          * RadMenuItem (dynamic; Header = "Product 3 Name")

    I have been playing around with various methods over the past few weeks, and the only way I have found to reliably change a static submenu RadMenuItem's Foreground brush is to set it directly:
    this.MyStaticRadMenuItem.Foreground = aBrushWithDifferentColor;

    However, this presents a problem: I am unable to access the dynamic submenu RadMenuItems until after the submenu has been shown for the first time (in other words, it looks like the RadMenuItems aren't created until the first time the user displays the submenu).

    Is there a way to force the static RadMenuItem to create its dynamic submenu RadMenuItems without waiting for the user to display the menu?


  2. Valeri Hristov
    Valeri Hristov avatar
    2252 posts

    Posted 16 Feb 2012 Link to this post

    Hi Jon,

    This is a Silverlight "feature" - the items in a Popup are created after the Popup is opened for the first time. The best option in my opinion is to create a view model for the menu and data-bind the RadMenu, as we demonstrate in our examples:
    In the view model for each menu item you could add a Foreground property and bind it to the corresponding RadMenuItem via ContainerBinding (Silverlight 4) or Binding in the ItemContainerStyle (Silverlight 5). This way you will work with the view models and your logic will not depend on the menu items.

    You could also try to attach a handler to the SubmenuOpened event of MyStaticRadMenuItem and update the Foreground of the dynamic items inside, or when the event handler was called at least once.

    Valeri Hristov
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Back to Top