I'm using the RichEditBox
to build a simple editor.
I found a piece of code which toggles bold text on a selection within the document window
private void RichEditBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
var state = Window.Current.CoreWindow.GetKeyState(Windows.System.VirtualKey.Control);
if ((state & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down)
{
if (e.Key == Windows.System.VirtualKey.B)
{
if (Editor.Document.Selection.Text.Length > 0)
{
ITextCharacterFormat format = Editor.Document.Selection.CharacterFormat;
format.Bold = FormatEffect.On;
}
else
{
// CTRL + B should toggle bold mode on or off here.
}
}
}
}
When I highlight a piece of text, and press CTRL+B
, it bolds the text. Result. However, anything I type after that point is also in bold.
This is not what I expected. According to the code above, I'm affecting the format of the Selection only.
When I select some text and press CTRL+B
it should toggle bold formatting on that selection and leave the default format as is.
I've tried using FormatEffect.Toggle
format.Bold = FormatEffect.Toggle
I've tried saving out the Document Character format first, then reapplying
ITextCharacterFormat original_format = Editor.Document.GetDefaultCharacterFormat();
ITextCharacterFormat format = Editor.Document.Selection.CharacterFormat;
format.Bold = FormatEffect.On;
Editor.Document.SetDefaultCharacterFormat(original_format);
This should reset the default back to what it was after bolding. But it doesn't
選択を何も設定せず、format.Bold = FormatEffect.Off
もう一度設定してからテキストを再選択することもできますが、それは長い道のりのようです (おそらくうまくいかないでしょう)。これを行う簡単な方法が必要ですか?
注:RichEditBoxタグがないため、これにRichTextBoxタグを付けました。担当者が 1500 人を超える人は追加できますか?