I have a simple WPF with a menu, a toolbar and a ListView in a GridView. The menu and toolbar actions are bound to commands.
I have <CommandBinding> that defines when commands should be executed (CanExecute). Some commands, such as “Create New Item” should always be executed, so they are bound to a “e.CanExecute = true;” function.
However, when the user selects all items in the list using Ctrl-A, and then presses Delete, my application runs a BackgroundWorker that deletes the items from the server and then sets the ItemsSource of the list view to the new data collection, which is empty.
This sometimes causes all commands in the menu and toolbar to be disabled. Note that their keyboard shortcuts still work, but the actions are disabled in the menu and toolbar.
This doesn’t always happen, and I failed to find a rule to when it does happen.
Did anyone run into a similar behavior or has any idea what might cause it?
Thanks.
Without code it may be hard. It looks like
CanExecute()is not called. Set breakpoint in it and check. I assume you are usingRoutedCommands. If yes, the problem may appear becauseMenuItem.CommandTargetis not set, and WPF tries to findCommandBindingssomewhere up the tree, beyond your actual command bindings. If this is the case, setCommandTargetto proper value. Also you may want to callCommandManager.InvalidateRequerySuggested()and see what happens.