Button enable/disable state not getting updated

May 22, 2008 at 7:56 PM

I've found that the DelegateCommand<T> isn't updating the enabled/disabled status for my buttons.  One caveat, it does the appropriate enabling/disabling the very first time the xaml is displayed, but after that it stays in whatever state it was when loaded.

What's strange is that my canExecuteMethod is returning the correct bool value and the executeMethod will only fire when appropriate.  It's just that the button's visual enabled state never changes.

One thing I did notice (through the use Debug.WriteLines) is that the canExecuteMethod is only being called when the button is clicked.  I compared this to other implementations of commanding I have and I think that the canExecuteMethod should be getting called alot more frequently than that - like whenever there's a UI change.

May 22, 2008 at 10:05 PM
Hi jmodzel,
DelegateCommand<T>, in comparison to RoutedCommand, does not respond to user input directly. You need to explicitly call a method on it (RaiseCanExecuteChanged) whenever your model changes (or the things that your command depends on).
As soon as you call that method, all your buttons will requery the command in order to see if the command can execute.
I hope this helps,

Julian Dominguez
Jun 2, 2008 at 4:33 PM
That worked, thanks.
Jul 14, 2008 at 2:41 PM

Hello All,

I witness similar behavior. I fixed an initial problem with my button because the CanExecute execution was not enabling my button whenever an item on my ListView was selected. This was resolved by calling the RaiseCanExecuteChanged for the DelegateCommand in the SelectionChanged event.

My latest issue with this is the following:

I have a ListView, Button, and ContextMenu on the ListView. I created a DelegateCommand which is implemented by the button and a menu item in the context menu. I also added a checkbox to monitor the availability of my menu item.

Observation:

  • At start, button and menu item (indicated by the checkbox) are disabled.
  • A selection is made.
  • The button is enabled and the menu item (as well as the checkbox) is disabled.
  • I make other selections, the menu item/checkbox is still disabled/unchecked
  • I make a selection by right clicking [accessing the context menu a second time], the checkbox/menuitem is enabled/checked.
  • As soon as I make another selection [left click an item], the menu item/checkbox is disabled/unchecked again.

This behavior is reoccurring. I do not understand why this occurs. I would think that the CommandParamenter is always observed upon invoking the CanExecute method.

Your advice is very much appreciated.

Andres Olivares