Access menu in menuRegion

Topics: Prism v4 - WPF 4
Nov 19, 2011 at 7:14 PM
Edited Nov 19, 2011 at 7:19 PM

newbie question.  I am building a Pism app that has multiple modules where each module will load thier own menus.  ModuleA has a number of views and each view has an associated menu that gets loaded in the MenuRegion when the module loads.  The current implementation that just loads the menus is working fine but the menu's are always enabled.  I want to enable the menu when the view is activated and disable the menu when the view is deactivated.  I define a view for each menu like the following:  The MenuRegion is defined as a Menu

<MenuItem x:Class="ModuleA.Views.ViewOneMenuView"
             Header="View One Menu">

            <MenuItem Header="Item A" />
            <MenuItem Header="Item B" />

I add the menu to the MainMenu region when the Module loads as follows:


Now I would like to enable or disable this menu when the associated view is enable/disabled.  So in the ViewOneViewModel I thought I could do something like the following:


public void OnNavigatedFrom(NavigationContext navigationContext)
    var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
    var region = regionManager.Regions["MainMenu"];
    UserControl menu = (UserControl)region.GetView("ViewOneMenuView");
    menu.IsEnabled = false;

This did not work since the menuItem being inserted into the MainMenu region is of type MenuItem which does not have a Name. and therfore region.getView() could not locate it.

How do I fix this?




Nov 19, 2011 at 9:25 PM

The following seems to solve my problem but I want to know if it is the correct way.  This works because there is only one ViewOneMenuView in the ManiMenu region.

        public void OnNavigatedTo(NavigationContext navigationContext)
            var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            var region = regionManager.Regions["MainMenu"];
            ViewOneMenuView menuView = (ViewOneMenuView)region.Views.First(p => p.GetType() == typeof(ViewOneMenuView));
            menu.IsEnabled = true;
Nov 21, 2011 at 2:41 PM
Edited Nov 21, 2011 at 2:48 PM


Based on my understanding of your scenario, the approach you mentioned seems to be a reasonable alternative.

Also you could benefit from using commanding instead of manually modifying the IsEnabled property of your visual component. In other words you could bind your menu button to a command (e.g a DelegateCommand), and react accordingly when navigating to another view by changing something in the view model where the command resides, that causes the CanExecute method of that command to change. After doing this you should call the RaiseCanExecuteChanged method, to make the command refresh its state on the UI.

For example, in the OnNavigatedTo method you could publish an event through the event aggregator, and subscribe to that event in your view model (where the command is placed), to react accordingly.

You can find more information about commanding in the Prism documentation at MSDN:

I hope you find this helpful.

Agustin Adami

Nov 22, 2011 at 5:51 PM

Thank you