views:

207

answers:

1

I have an Windows.Documents.InlineUIContainerin a RichTextBox, and sometimes it's font size of alignment change when I hit key combination such as Ctrl+Space. I couldn't find any place to handle these events and block them somehow. I don't want to block it in the RichTextBox. I am more looking for a way to block it only on the InlineUIContainer.

A: 

InlineUIContainer is a FrameworkContentElement, so it participates in all the normal event routing. So to block command routing need to do is use CommandManager.AddExecutedHandler (or equivalently AddHandler(CommandManager.ExecutedEvent)) on the InlineUIContainer and mark the commands as Handled.

container.AddHandler(CommandManager.ExecutedEvent, new ExecutedRoutedEventHandler((obj, e) =>
{
  var command = e.Command as RoutedCommand;
  if(command!=null && command.OwnerType==typeof(EditingCommands))
    e.Handled = true;
}));

Alternatively the same handler can be added to your inline UI content (InlineUIContainer.Content) if it easier to do it that way.

Note that the above code blocks all EditingCommands, but you can block any other commands as desired.

Ray Burns
CommandManager.AddExecutedHandler(inlineUIContainer, ExecutedCustomCommand) doesn't work as it requires an UIElement (not an InlineUIContainer). Any suggestion?
paradisonoir
Sorry about that. CommandManager.AddExecutedHandler is a convenience method that simplifies the syntax for adding an executed handler. I forgot that it only works for UIElements, not ContentElements. I have updated the code to directly call AddHandler, which will work for both UIElements and ContentElements.
Ray Burns
Also note that you have the choice of setting this event on either the InlineUIContainer or on the UIElement it contains. Either solution will produce the same result, but one or the other may be easier to implement depending on how your code is structured.
Ray Burns
That looks promising, but when I gave it a try by trying ctr+space or any other commands, like ctr+U/B/Left/Right, and etc. it didn't even enter the handler. Does it work for you for any type of command?
paradisonoir