locked
Lose focus on a button, after binded command execution, in MVVM with Prism RRS feed

  • Question

  • I have a WPF application developed according to the MVVM framework with Prism and Unity.

    In the View I have a button binded to a specific command in the ViewModel, with _container is a Unity container implementation:

    <Button Command="{Binding SaveCommand}"
            Content="Save"
            Visibility="{Binding Path=CanSaveCommand,
                         Converter={StaticResource ResourceKey=BoolToCollapseConverter}}" />
    SaveCommand = new DelegateCommand(HandleSaveCommand);
    
    private void HandleSaveCommand()
    {
    _container.Resolve<IEventAggregator>().GetEvent<ShowBusyEvent>().Publish(true);
    Save();
    _container.Resolve<IEventAggregator>().GetEvent<ShowBusyEvent>().Publish(false);
    }

    All the command operations are performed correctly but when its handler's execution finishes, the button keeps on being pressed with the light blue border around it.

    Of course if I repress the button its functionality is not affected but for a matter of user experience I would like to know a way to tell the View that the button can be shown as "not clicked". Is a good way to use the ClickMode property of the button and bind it to a property in the ViewModel triggered once the command's handler finish its execution? Thanks!


    • Edited by Trinakriae Monday, April 28, 2014 3:44 PM
    Monday, April 28, 2014 3:43 PM

Answers

  • You could simply call the Keyboard.ClearFocus() method in the HandleSaveCommand() method in the view model:

            private void HandleSaveCommand()
            {
                _container.Resolve<IEventAggregator>().GetEvent<ShowBusyEvent>().Publish(true);
                Save();
                _container.Resolve<IEventAggregator>().GetEvent<ShowBusyEvent>().Publish(false);
    
                Keyboard.ClearFocus();
            }
    

    The ClickMode property only specifies when the Click event should be raised: http://msdn.microsoft.com/en-us/library/system.windows.controls.clickmode(v=vs.110).aspx

    • Marked as answer by Trinakriae Tuesday, April 29, 2014 11:37 AM
    Tuesday, April 29, 2014 10:28 AM

All replies

  • http://stackoverflow.com/questions/16417123/lost-focus-event-strategy-in-mvvm-pattern
    Tuesday, April 29, 2014 10:08 AM
  • You could simply call the Keyboard.ClearFocus() method in the HandleSaveCommand() method in the view model:

            private void HandleSaveCommand()
            {
                _container.Resolve<IEventAggregator>().GetEvent<ShowBusyEvent>().Publish(true);
                Save();
                _container.Resolve<IEventAggregator>().GetEvent<ShowBusyEvent>().Publish(false);
    
                Keyboard.ClearFocus();
            }
    

    The ClickMode property only specifies when the Click event should be raised: http://msdn.microsoft.com/en-us/library/system.windows.controls.clickmode(v=vs.110).aspx

    • Marked as answer by Trinakriae Tuesday, April 29, 2014 11:37 AM
    Tuesday, April 29, 2014 10:28 AM
  • Thanks you very much, this code does its work like a charm
    Tuesday, April 29, 2014 11:38 AM