In WPF RichTextBox, does overriding of Underline/Strikethrough work?
-
23-09-2019 - |
Question
In a WPF RichTextBox
, the effective style of a Run
of text is a result of combining the properties defined on the Run
with the properties it "inherits" from the enclosing Paragraph
and finally the styles on the Document
. So you can set FontWeight
to Bold
at any of those levels. You can also set it Bold
on the Paragraph
and then switch it to Normal
(override it) for a specific Run
.
However, underline and strikethrough are different. They are items that can optionally appear in a list of TextDecorations
, which is a property of Inline
(and hence Run
) and of Paragraph
, but not of Document
. And you can switch on Underline
in the Paragraph
, and it gets inherited so that all Run
s within that Paragraph
default appear underlined by default.
Is it possible to switch it off underline in a specific Run
? i.e. is there a way to insert an entry into the list of TextDecorations
which would mean "Don't underline", thus overriding the Paragraph
's setting?
Solution
I know that you can basically handle the underline like this:
protected void ContinueRoutingCommand(object sender, CanExecuteRoutedEventArgs e)
{
else if (e.Command == EditingCommands.ToggleUnderline)
{
e.Handled = true;
e.CanExecute = true;
e.ContinueRouting = false;
return;
}
}
However, in your case, you want to override it only in a specific Run, right? Then, you can verify your Run inside the inner if and block it exactly like this if you want.