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

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

1 Answer 197 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Jon
Top achievements
Rank 1
Jon asked on 13 Feb 2012, 09:48 PM
Hello,

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?

Thanks,

Jon

1 Answer, 1 is accepted

Sort by
0
Valeri Hristov
Telerik team
answered on 16 Feb 2012, 02:50 PM
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:
http://demos.telerik.com/silverlight/#Menu/BoundaryDetection
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.

Regards,
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 >>
Tags
Menu
Asked by
Jon
Top achievements
Rank 1
Answers by
Valeri Hristov
Telerik team
Share this question
or