I have implemented a toolbar that can change the font size, family, color, etc. I found that details can be complex with wpf richtextbox. Setting the selection font makes sense, but there are also default font properties for the text box and current carriage properties that you can deal with. Here is what I wrote to make it work in most cases with font size. The process should be the same for fontfamily and fontcolor. Hope this helps.
public static void SetFontSize(RichTextBox target, double value)
{
if (target == null)
return;
if (target.Selection != null)
{
if (target.Selection.IsEmpty)
{
if (target.Selection.Start.Paragraph == null)
{
Paragraph p = new Paragraph();
p.FontSize = value;
target.Document.Blocks.Add(p);
}
else
{
TextPointer curCaret = target.CaretPosition;
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;
Run newRun = new Run();
newRun.FontSize = value;
curParagraph.Inlines.Add(newRun);
target.CaretPosition = newRun.ElementStart;
}
}
}
else
{
TextRange selectionTextRange = new TextRange(target.Selection.Start, target.Selection.End);
selectionTextRange.ApplyPropertyValue(TextElement.FontSizeProperty, value);
}
}
target.Focus();
}