Guard/IsEnabled doesn't work until button is clicked

Topics: Actions & Coroutines
Oct 23, 2011 at 12:14 AM

On my shell view, I'm using Telerik's RibbonView with a few RadRibbonButtons on it, along with "Message.Attach" to call various viewmodel methods.  I have guard properties in the "CanXXX" form.  When I first start the application, the ribbon buttons always show up enabled, even if the corresponding guard properties are properly returning "false."  What's strange is that if I click on a button, it seems to immediately fire off a re-evaluation of the guard (CM log output shows "Action: XXX availability update.") and the button becomes disabled (the viewmodel method is not called).  After this, any guard property changes are respected and everything seems to work.

I've tried calling a viewmodel "Refresh()" in an OnViewLoaded override, but it doesn't work.  Various attempts to get the ribbon or the buttons in the view to refresh themselves haven't worked either.

Any ideas?

  Vikram

Oct 23, 2011 at 8:19 PM

Are you doing any sort of NotifyOfPropertyChange(()=>GuardProperty);  somewhere?  code snippet, how you set things up would help, as for the Guard Property itself how do you say its true?

Oct 26, 2011 at 11:23 PM

Sorry, should have thought to put in a snippet when I posted. 

All the guard properties look like this:

 

        public bool CanShowSettings
        {
            get
            {
                return _canShowSettings;
            }
            set
            {
                _canShowSettings = value;
                NotifyOfPropertyChange( () => CanShowSettings );
            }
        }

 

The properties are set to true/false as usual, by using the set accessor.  Snoop shows that the properties are set correctly.

Googling reveals that some people are having similar problems with IsEnabled and ICommands, and that the suggested fix is to use CommandManager.InvalidRequerySuggested().   No similar problems with CM's messages that I could find.

   Vikram

Oct 27, 2011 at 5:10 AM

try dropping a notifyofpropertychange in OnViewLoaded for CanShowSettings property...

Oct 28, 2011 at 12:17 AM

I tried NotifyOfPropertyChange in OnViewLoaded, as well as a Refresh -- neither works.

Vikram

Oct 28, 2011 at 3:29 PM
Edited Oct 28, 2011 at 3:30 PM

how do you set CanShowSettings?  Are you doing it on variable init? Or are you setting prior to application display based on some other logic? 

Nov 6, 2011 at 5:29 AM

Finally figured this one out -- and the answer is so obvious I'm embarrassed.

The ribbon buttons are bound to button viewmodels that are exposed as properties on the shell viewmodel.  Calling a Refresh on each button viewmodel (not the shell viewmodel) gets the enabled/disabled state working correctly; so for the example above, calling ShowSettingsViewModel.Refresh() gets it all sorted out.

Hope this saves somebody else some time

   Vikram