Binding to Dynamic Data
The data displayed in the RadMenu has a hierarchical structure (similar to the RadTreeView). This means that each item may come with a set of items on its own. For that reason you have to use the HierarchicalDataTemplate. This tutorial will walk you through the most important steps in creating, configuring and applying a HierarchicalDataTemplate to your RadMenu, as follows:
Preparing the Data
For this tutorial the following sample class will be used:
Example 1: MenuItem sample class
The MenuItem class holds the information for the menu items.
Text - represents the text value for the item.
IconUrl - represents the url of the image that represents the icon of the menu item.
SubItems - a collection of the sub menu items of the current menu item.
On the next step a sample method, which creates the sample data, is needed. Here is one:
Example 2: Creating MenuItems collection
Finally, pass the generated collection to the ItemsSource of the RadMenu.
Example 3: Setting the ItemsSource of the Menu
Creating and Applying the ItemContainerStyle
In order to visualize the data in RadMenu you can use ItemContainerStyle property. When used in combination with ItemsSource, the Style is applied on all RadMenuItems. The only thing you have to do is set the bindings for the needed properties. For the "Icon" you will have to use "IconTemplate" as shown below. When you bind the "Icon" property to the "IconUrl", its value will be passed to the DataTemplate and used as a Source of an image.
Example 4: Defining MenuItemStyle
If you're using Implicit Styles note that the Style that targets RadMenuItem should be based on the default RadMenuItemStyle.
So it's time to define RadMenu and set "ItemContainerStyle" property:
Example 5: Applying the MenuItemStyle to RadMenu
Figure 1 illustrates the final result.