A workaround is to queue another message in your Executed handler:
void copy_Executed(object sender, EventArgs e)
{
Dispatcher.BeginInvoke(new ThreadStart(delegate()
{
//do update of bold button here
}), null);
}
This will ensure your work is added to the back of the queue, and will be executed after other messages of the same or higher priority.
However, I'd like to suggest a better solution. If you think about it, the bold button is responsible for executing two different commands: make bold, and make normal. It switches between these two commands based on the currently selected text/caret position. Therefore, you could write a custom ICommand
implementation that encapsulates two sub-commands (completely untested code):
public class CompositeCommand : ICommand
{
private readonly ICommand _command1;
private readonly ICommand _command2;
private ICommand _activeCommand;
public CompositeCommand(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);
}
}
You can then construct a CompositeCommand
with two commands: one to bolden and one to unbolden text. Then you can bind the Button
in your UI to the ActiveCommand
property to change it in anyway you like based on what will happen when you click the command. For example, if you're using a ToggleButton
you would bind IsChecked
to ActiveCommand
and convert to true
is the active command is unbolden. Of course, the bolden and unbolden commands need CanExecute
logic of their own that inspects the selected text.
HTH,
Kent