-1

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 人を超える人は追加できますか?

4

2 に答える 2

0

私はうまくいくハックを見つけました。行き詰まっている人への回答としてこれを投稿していますが、より良い回答があるはずなので、回答を受け入れていません。

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)
            {
                // text is selected. make it bold
                ITextCharacterFormat format = 
                     Editor.Document.Selection.CharacterFormat;
                format.Bold = FormatEffect.On;

                var start_pos = Editor.Document.Selection.StartPosition;
                Editor.Document.Selection.StartPosition = 
                     Editor.Document.Selection.EndPosition;
                format.Bold = FormatEffect.Off;

                // Editor.Document.Selection.StartPosition = start_pos;   
                // this is where I was re-selecting the text after switching bold OFF
                // but doing so switches it back on again. which makes no sense                     
            }
            else
            {
                // no text selected. just enable bold mode
                ITextCharacterFormat format = 
                     Editor.Document.Selection.CharacterFormat;
                format.Bold = FormatEffect.Toggle;
            }   
        }
    }
}

テキストを選択して太字にした後、自動的に選択が解除されるため、これは完璧ではありません。しかし、実際には、これは実際にはうまく機能することがわかりました。それでも、ハックなので、ハックのように感じます

于 2013-11-08T17:05:12.667 に答える