Event triggering after command execution

I am looking for a way to fire an event after running a command. I work with EditingCommands (ToggleBold, ToggleItalic ... ect.) And would like to be able to have a method called immediately after the command finishes everything that it does. An example would be that I have selected text and press Ctrl + B and it will execute the Edit Commands. ToggleBold. Immediately after switching the text, I want to call a method that will update the ToggleButton associated with the FontWeight selection. I tried to use the Executed event, but, unfortunately, for me it was called before the text is affected, and therefore will update the button with information that will change in just a second. Does anyone know about this? Thanks for any help!

Greetings

+4
source share
2 answers

The workaround is the next message queue in the Executed handler:

void copy_Executed(object sender, EventArgs e) { Dispatcher.BeginInvoke(new ThreadStart(delegate() { //do update of bold button here }), null); } 

This will ensure that your work is added to the back of the queue and will be executed after other messages with the same or higher priority.

However, I would like to offer a better solution. If you thought about it, the bold button is responsible for executing two different commands: make it bold and make it normal. It switches between the two teams based on the currently selected text / caret position. Therefore, you can write a custom implementation of ICommand that encapsulates two subcommands (completely untested code):

 public class TogglingCommand : ICommand { private readonly ICommand _command1; private readonly ICommand _command2; private ICommand _activeCommand; public TogglingCommand(ICommand command1, ICommand command2) { _command1 = command1; _command2 = command2; } public ICommand ActiveCommand { get { return _activeCommand; } } public bool CanExecute(object parameter) { if (_command1.CanExecute(parameter)) { _activeCommand = _command1; } else if (_command2.CanExecute(parameter)) { _activeCommand = _command2; } else { _activeCommand = null; } return _activeCommand != null; } public void Execute(object parameter) { _activeCommand.Execute(parameter); } } 

Then you can build a TogglingCommand with two commands: one for bolden and one for unlocking text. You can then bind Button in your user interface to the ActiveCommand property to change it anyway based on what happens when you press the command. For example, if you use ToggleButton , you must bind IsChecked to ActiveCommand and convert the active unbolden command to true. Of course, the bolden and unbolden commands need their own CanExecute logic, which checks the selected text.

+4
source

Can you use the Executed routed event handler (elapsed time)? (Or maybe what you say you tried)

 public partial class CustomerWindow : Window { public CustomerWindow() { InitializeComponent(); CommandBinding binding = new CommandBinding(ApplicationCommands.Copy); binding.Executed += new ExecutedRoutedEventHandler(this.copy_Executed); this.CommandBindings.Add(binding); } void copy_Executed(object sender, RoutedEventArgs e) { MessageBox.Show("Executed the Copy command"); } } 
0
source

All Articles