1

I am working on a Composite MVVM application and trying to get Global Binding events happening - Except it is NOT!..

the buttons are disabled by default although the CanRun returns true!! !! I have followed the Composite Guide and the OnLoadMenu is not firing!!!

I have been going around in circles (Event Aggregators, DelegateCommands, Composite Commands) It just isnt working. Can any please look at this and tell me what I am Missing??

//xmlns:local="clr-namespace:Commands;assembly=MyApp"
 <Button HorizontalAlignment="Center" Margin="1,1,1,1" 
                               Grid.Row="2" 
            Command="{x:Static local:AdminGlobalCommands.LoadAdminMenu}"/>


 public static class AdminGlobalCommands // In Common Code Library
    {
        //List All Global Commands Here
        public static CompositeCommand LoadAdminMenu = new CompositeCommand();
    }


 public class AdminModuleViewModel : ViewModelBase, IAdminModuleViewModel // In AdminModule
    {
        protected IRegionManager _regionManager;
        private IUnityContainer _container;

        public AdminModuleViewModel(

            IEventAggregator eventAggregator,
            IBusyService busyService,
            IUnityContainer container,
            IRegionManager regionManager
          )
            : base(eventAggregator, busyService, container)
        {
            // show the progress indicator
            busyService.ShowBusy();
            this._regionManager = regionManager;
            this._container = container;

            //set up the command receivers
            this.AdminShowMenuCommand =  new DelegateCommand<object>(this.OnLoadAdminMenu, this.CanShowAdminMenu);

            //Listen To Events
            AdminGlobalCommands.LoadAdminMenu.RegisterCommand(AdminShowMenuCommand);

            busyService.HideBusy();
        }
        public DelegateCommand<object> AdminShowMenuCommand { get; private set; }

        private bool CanShowAdminMenu(object  obj) 
        { //Rules to Handle the Truth
            return true; 
        }

        public void OnLoadAdminMenu(object obj)
        {
            UIElement viewToOpen = (UIElement)_container.Resolve(typeof(AdminMenuControl)) ;
            _regionManager.AddToRegion("MainRegion", viewToOpen);
            _regionManager.Regions["MainRegion"].Activate(viewToOpen); ;
        }
    }
Traci
  • 908
  • 1
  • 13
  • 31
  • Where are you constructing AdminModuleViewModel? How is it getting set to a DataContext? Also, do you actually need the "global-ness" of the command or are you just trying to wire this one Button objects command to the handler in your ViewModel? If so I can post an easier way to accomplish that – Brad Cunningham Feb 25 '10 at 18:29
  • The data context is set via prism resolve... In this example I want the command to run from this Module. e.g it is composite and the shell does not know about the AdminModule so I cannot put it in the Primary Application Thus the Globl Commands Class. (the global commands class is in the application infrastructure and also is not "aware" of the application module. I am trying to acheive from the admin module, if the command is triggered from anywhere in the application, the Menu will show. – Traci Feb 26 '10 at 04:56

1 Answers1

4

When using PRISM if you are creating a CompositeCommand with monitorCommandActivity set to true, you also need to be aware of and set DelegateCommand.IsActive state.

In that case CompositeCommand will not consider inactive DelegateCommands and as a result your button might stay disabled (for example when no other active and executable DelegateCommand is in the CompositeCommands's command chain).

John
  • 41
  • 2