RichEditBox:使用CTRL+I设置斜体文本删除文本 [英] RichEditBox: Using CTRL+I to set italic text deletes the text

查看:53
本文介绍了RichEditBox:使用CTRL+I设置斜体文本删除文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码可以将 RichEditBox 中的文本加粗和斜体:

I have the following code to bold and italicise 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()ItaliciseText() 也可以从工具栏上的按钮调用.

BoldText() and ItaliciseText() are also called from buttons on a toolbar.

当按下Bold 时,所选文本将正确格式化为粗体文本.

When Bold is pressed, the selected text is formatted to Bold text correctly.

当按下 CTRL+B 时,所选文本将正确格式化为粗体文本.

When CTRL+B is pressed, the selected text is formatted to Bold text correctly.

当按下Italic 时,所选文本将正确格式化为斜体文本

When Italic is pressed, the selected text is formatted to Italic text correctly

当按下CTRL+I时,选中的文本被正确格式化为斜体文本,但随后被删除

When CTRL+I is pressed, the selected text is formatted to Italic text correctly, but then deleted

我知道格式正在发生,因为如果我按 CTRL+Z,文本将以斜体返回.CTRL+I 在 selectedText.CharacterFormat = charFormatting; 之后引起额外的操作,它会擦除​​文本.

I know the formatting is happening because if I press CTRL+Z the text returns in italics. CTRL+I is causing an additional operation after selectedText.CharacterFormat = charFormatting; which wipes the text.

我无法弄清楚为什么会发生这种情况,因为当从工具栏上的按钮触发时,代码几乎与完美无瑕的 BoldText() 代码和文字完全相同.

I can't figure out why this is happening as the code is almost identical to the flawless BoldText() code and words perfectly when fired from a button on the toolbar.

有什么想法吗?

推荐答案

Ctrl+I 上可能有另一个处理程序(可能是导致文本删除的处理程序),请考虑设置 KeyRoutedEventArgs.Handled = true;,参见 this,(在本例中为 e.Handled=true;),当您不希望在您的以外的其他地方处理此 KeyEvent 时.

Ctrl+I may have another handler on it (which might be one causing text deleted), consider setting KeyRoutedEventArgs.Handled = true;, see this, (in this case e.Handled=true;) when you don't want this KeyEvent to be handled else where other than yours.

这篇关于RichEditBox:使用CTRL+I设置斜体文本删除文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆