I have the following code for bold and selected text in a RichEditBox:
private async void Page_KeyDown(object sender, KeyRoutedEventArgs e)
{
var state = Window.Current.CoreWindow.GetKeyState(Windows.System.VirtualKey.Control);
if ((state & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down)
{
switch (e.Key)
{
case Windows.System.VirtualKey.B:
await BoldText();
break;
case Windows.System.VirtualKey.I:
await ItaliciseText();
break;
}
}
}
private async Task BoldText()
{
ITextSelection selectedText = editor.Document.Selection;
if (selectedText != null)
{
ITextCharacterFormat charFormatting = selectedText.CharacterFormat;
charFormatting.Bold = FormatEffect.Toggle;
selectedText.CharacterFormat = charFormatting;
}
}
private async Task ItaliciseText()
{
ITextSelection selectedText = editor.Document.Selection;
if (selectedText != null)
{
ITextCharacterFormat charFormatting = selectedText.CharacterFormat;
charFormatting.Italic = FormatEffect.Toggle;
selectedText.CharacterFormat = charFormatting;
}
}
BoldText()and ItaliciseText()also called from the buttons on the toolbar.
When clicked, the Boldselected text will be correctly formatted in bold.
When pressed, the CTRL+Bselected text will be correctly formatted for bold text.
When clicked, the Italicselected text is correctly formatted in italic text
When clicked, the CTRL+Iselected text is correctly formatted in italic text , but then deleted
, , , CTRL + Z, . CTRL + selectedText.CharacterFormat = charFormatting;, .
, , BoldText() , .
?