6 c# wpf richtextbox
我目前正在尝试在WPF项目中创建一些基本的文字处理器功能。我使用的是RichTextBox,并且知道所有的EditingCommands(ToggleBold,ToggleItalic ... ect)。我坚持的事情是允许用户更改字体大小和字体,例如在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);
Run Code Online (Sandbox Code Playgroud)
所以我的问题是我应该怎么做?有没有更好/更方便的方法来做到这一点?我曾经想过,我需要在段落中插入新的内联,但是我不知道该怎么做。任何帮助表示赞赏。谢谢。
尝试这个
var range = new TextRange( richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd );
range.ApplyPropertyValue( TextElement.FontSizeProperty, value );
Run Code Online (Sandbox Code Playgroud)