How to create Dynamic Menu from the modules

Topics: Prism v4 - WPF 4
Dec 21, 2011 at 6:32 PM

I am new to the Composite, i am developing application in that i want to create menu after loading modules and separate that under particular header menu.

As we seen 

File         Edit

New       Cut

Open     Copy


Like this but it has to be done dynamically, So how we can do that, please answer me if anyone know.

Dec 21, 2011 at 7:37 PM


In WPF the controls Menu and MenuItem inherit from ItemsControl. Based on my understanding, as Prism provides a region adapter for ItemsControls, you could define the Menu (or MenuItem) where you want to add the MenuItems of the modules as a region. Then, you could inject or register a MenuItem in the region (like you would do with a view).

For example, suppose that the shell has a menu which we registered as a region with the name "MenuRegion" and we want to add a MenuItem to the aforementioned menu when a module initializes. This could be achieved doing something like this:

public void Initialize()
    // We create a MenuItem to register in the "MenuRegion."

     MenuItem subItem = new MenuItem();
     subItem.Header = "Click Me";
     subItem.Click += (object sender, RoutedEventArgs e) => { MessageBox.Show("You clicked the \"Click Me\" button."); };
     this.regionManager.RegisterViewWithRegion("MenuRegion", () => subItem);

You can find more information about injecting and registering views in regions in the following chapter of the Prism documentation:

I hope you find this useful,

Damian Cherubini