byte
byte

Reputation: 1685

Wpf button click when application window is not active

I have a WPF application window with a set of three buttons (Button A, B, C) and these buttons have command binding to respective View Model property.

When Button A is clicked it's command executes (another application is launched, Button A is disabled as CommandCanExecute condition is updated to return false). This results in application window being deactivated.

Clicking Button B doesn’t execute it's command but instead only activates the window and sets the focus to the Main window. 2nd click is required on Button B to execute its command.

The desired functionality is on click of Button B, while the application window is not active, the application window activates and command associated with Button B is executed. I have also tried removing the command and adding a click handler for test purpose but this displays same behaviour.

I appreciate any help with this issue.

Upvotes: 0

Views: 752

Answers (2)

byte
byte

Reputation: 1685

Identified the problem to be related to how our Command binding is setup.

Solution is to fire CanExecuteChanged event so that relevant CommandCanExecute is re-evaluated by the framework. I replaced our ICommand implementation with Prism DelegateCommand and invoked RaiseCanExecuteChanged() method of delegate command when the condition is changed.

Before using Prism DelegateCommand, I tried calling CommandManager.InvalidateRequerySuggested() but it didn't help in this case. I have not fully investigated why but we are already using PRISM in other applications so using DelegateCommand will suffice here.

Many thanks

Upvotes: 0

ΩmegaMan
ΩmegaMan

Reputation: 31616

Check out Executing Command Logic in a View Model and see if the DataContextCommandAdapter described in later part of the article can chain/call the commands needed for button B.

Upvotes: 1

Related Questions