WPF RichTextBox:如何更改选定的文本字体?

WPF RichTextBox:如何更改选定的文本字体?,wpf,richtextbox,Wpf,Richtextbox,如何更改WPF RichTextBox中当前选定文本区域的字体?类似以下内容如何: TextSelection text = richTextBox.Selection; if (!text.IsEmpty) { text.ApplyPropertyValue(RichTextBox.FontSizeProperty, value); } 我已经实现了一个工具栏,可以更改字体大小、族、颜色等。我发现wpf richtextbox的细节可能很棘手。设置选择字体有一定的意

如何更改WPF RichTextBox中当前选定文本区域的字体?

类似以下内容如何:

 TextSelection text = richTextBox.Selection; 
 if (!text.IsEmpty) 
 { 
     text.ApplyPropertyValue(RichTextBox.FontSizeProperty, value); 
 }

我已经实现了一个工具栏,可以更改字体大小、族、颜色等。我发现wpf richtextbox的细节可能很棘手。设置选择字体有一定的意义,但是,还有文本框的默认字体属性和当前插入符号属性需要处理。下面是我写的,让它在大多数情况下都能使用字体大小。fontfamily和fontcolor的过程应相同。希望能有帮助

    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();
    }

要更改RichTextBox中所选内容的字体系列,应使用以下命令:

text.ApplyPropertyValue(Run.FontFamilyProperty, value);
RichTextBox中的选定文本是运行对象,因此必须使用运行依赖项属性。
这似乎至少在Silverlight中起作用,因此在WPF中也应该如此。

要获得当前选择,请使用:

Dim rng As TextRange=新的TextRange(YourRtfBox.Selection.Start,YourRtfBox.Selection.End)

然后设置字体样式:

rng.ApplyPropertyValue(Inline.FontSizeProperty,YourFontSizeValue) rng.ApplyPropertyValue(Inline.FontFamilyProperty,YourFontFamilyValue)

已解决

if (this.TextEditor.Selection.IsEmpty)
    this.TextEditor.CurrentFontFamily = SelectedFont;
else
    this.TextEditor.Selection.ApplyPropertyValue(TextElement.FontFamilyProperty, SelectedFont);

这至少是字体大小。如果你想改变字体家族的属性,那么字体家族可能有一个属性。它适用于字体大小。但对于字体系列,它会更改整个段落,而不仅仅是选择。此解决方案基于: