views:

1036

answers:

2

Hello, I am currently trying to create some basic word processor features in a WPF project. I am using a RichTextBox and am aware of all of the EditingCommands (ToggleBold, ToggleItalic...ect.). The thing I am stuck on is allowing the user to change the fontsize and font face like in MS Office where the value changes for only the selected text and if there is no selected text then the value will change for the current caret position. I have come up with a decent amount of code to get this to work, but am having problems with the no selected text thing. Here is what I am doing for the 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);

So my question is how should I go about doing this? Is there a better/more convenient way to do this? One thought I had was that I would need to insert a new Inline into the Paragraph but I couldn't figure out how to do that. Any help is appreciated. Thank you.

A: 

Try this

var range = new TextRange( richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd );
range.ApplyPropertyValue( TextElement.FontSizeProperty, value );
bendewey
A: 

I just had the exact same problem. I was able to use TextElement.FontSizeProperty as bendewey said. However, it still wasn't working quite right. After looking through the project at the link below, I found out I was still doing something wrong. I wasn't setting the focus back to the RichTextBox... which the author of the project below didn't need to do because they were using a ribbon combobox. With a regular combobox, after you select a font, it does actually change the font of the selection in the RichTextBox, but it takes the focus away from the RTB. When you click back on the RTB to get the focus and start typing, you have a new selection object, in which case the font is back to the default font of the RTB itself.

http://www.codeplex.com/WPFRichEditorLibrary

sub-jp