6

現在、WPF プロジェクトでいくつかの基本的なワード プロセッサ機能を作成しようとしています。私は RichTextBox を使用しており、すべての EditingCommands (ToggleBold、ToggleItalic など) を認識しています。私が立ち往生しているのは、MS Office のようにユーザーがフォントサイズとフォントフェイスを変更できるようにすることです。選択したテキストのみの値が変更され、選択したテキストがない場合は現在のキャレット位置の値が変更されます。これを機能させるためのかなりの量のコードを思いつきましたが、テキストが選択されていないことに問題があります。これが、RichTextBox.Selection に対して行っていることです。

TextSelection text = richTextBox.Selection;
if (text.IsEmpty)
{
    //doing this will change the entire word that the current caret position
    //is on which is not the desire/expected result.
    text.ApplyPropertyValue(RichTextBox.FontSizeProperty, value);
}
else
    //This works as expected.
    text.ApplyPropertyValue(RichTextBox.FontSizeProperty, value);

だから私の質問は、これを行うにはどうすればよいですか? これを行うためのより良い/より便利な方法はありますか? 私が考えていたのは、新しいインラインを段落に挿入する必要があるということでしたが、その方法がわかりませんでした。どんな助けでも大歓迎です。ありがとうございました。

4

7 に答える 7

4

私はこれを次のように解決しました:

TextRange r = new TextRange(richtextbox.Selection.Start, richtextbox.Selection.End);
r.ApplyPropertyValue(TextElement.FontSizeProperty, value);
于 2014-10-22T16:58:56.877 に答える
2

私はまったく同じ問題を抱えていました。私はbendeweyが言ったようにTextElement.FontSizePropertyを使うことができました。しかし、それでも正しく機能していませんでした。以下のリンクでプロジェクトを調べた後、私はまだ何か間違ったことをしていることに気づきました。私はフォーカスをRichTextBoxに戻していませんでした...以下のプロジェクトの作成者はリボンコンボボックスを使用していたため、これを行う必要はありませんでした。通常のコンボボックスでは、フォントを選択した後、RichTextBoxで選択したフォントが実際に変更されますが、RTBからフォーカスが外れます。RTBをクリックしてフォーカスを取得し、入力を開始すると、新しい選択オブジェクトが作成されます。この場合、フォントはRTB自体のデフォルトフォントに戻ります。

http://www.codeplex.com/WPFRichEditorLibrary

于 2009-12-22T21:37:39.223 に答える
1

これを試して

var range = new TextRange( richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd );
range.ApplyPropertyValue( TextElement.FontSizeProperty, value );
于 2009-03-23T17:31:38.117 に答える
0

私があなたを正しく理解しているなら、私は同様の問題を抱えていました。近いものの、うまくいかなかったさまざまな答えをたくさん探してみました。私の問題は、明示的に選択されたテキストに対してフォントの変更がうまく機能することでしたが、選択されたテキストがなく、フォントサイズが変更された場合、入力された次のテキストはデフォルトのフォントサイズに戻ります。これが私が最終的に理解したものです。試してみて、他の誰かにうまくいったかどうか教えてください:

    public static void SetFontSize(RichTextBox target, double value)
    {
        // Make sure we have a richtextbox.
        if (target == null)
            return;

        // Make sure we have a selection. Should have one even if there is no text selected.
        if (target.Selection != null)
        {
            // Check whether there is text selected or just sitting at cursor
            if (target.Selection.IsEmpty)
            {
                // Check to see if we are at the start of the textbox and nothing has been added yet
                if (target.Selection.Start.Paragraph == null)
                {
                    // Add a new paragraph object to the richtextbox with the fontsize
                    Paragraph p = new Paragraph();
                    p.FontSize = value;
                    target.Document.Blocks.Add(p);
                }
                else
                {
                    // Get current position of cursor
                    TextPointer curCaret = target.CaretPosition;
                    // Get the current block object that the cursor is in
                    Block curBlock = target.Document.Blocks.Where
                        (x => x.ContentStart.CompareTo(curCaret) == -1 && x.ContentEnd.CompareTo(curCaret) == 1).FirstOrDefault();
                    if (curBlock != null)
                    {
                        Paragraph curParagraph = curBlock as Paragraph;
                        // Create a new run object with the fontsize, and add it to the current block
                        Run newRun = new Run();
                        newRun.FontSize = value;
                        curParagraph.Inlines.Add(newRun);
                        // Reset the cursor into the new block. 
                        // If we don't do this, the font size will default again when you start typing.
                        target.CaretPosition = newRun.ElementStart;
                    }
                }
            }
            else // There is selected text, so change the fontsize of the selection
            {
                TextRange selectionTextRange = new TextRange(target.Selection.Start, target.Selection.End);
                selectionTextRange.ApplyPropertyValue(TextElement.FontSizeProperty, value);
            }
        }
        // Reset the focus onto the richtextbox after selecting the font in a toolbar etc
        target.Focus();
    }
于 2010-11-16T21:12:02.637 に答える