WPF RichTextBox:如何更改选定的文本字体?
如何更改 WPF RichTextBox 中当前选定文本区域的字体?
how can I change the font for a currently selected text area inside a WPF RichTextBox?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我已经实现了一个可以更改字体大小、系列、颜色等的工具栏。我发现 wpf richtextbox 的细节可能很棘手。设置选择字体有一定意义,但是,还需要处理文本框的默认字体属性和当前插入符号属性。这是我写的内容,让它适用于大多数情况下的字体大小。 fontfamily 和 fontcolor 的过程应该相同。希望有帮助。
I've implemented a toolbar that can change the font size, family, color, etc. What i found is the details can be tricky with the wpf richtextbox. Setting the selection font makes some sense, but, there are also the default font properties of the text box, and the current caret properties to contend with. Here is what i've written to get it to work for most cases with the font size. The process should be the same for fontfamily and fontcolor. Hope it helps.
怎么样:
How about something like:
解决了...
Solved...
要获取当前选择,请使用:
Dim rng As TextRange = New TextRange(YourRtfBox.Selection.Start, YourRtfBox.Selection.End)
然后设置字体样式:
rng.ApplyPropertyValue(Inline.FontSizeProperty, YourFontSizeValue)
rng.ApplyPropertyValue(Inline.FontFamilyProperty, YourFontFamilyValue)
To get the current selection use:
Dim rng As TextRange = New TextRange(YourRtfBox.Selection.Start, YourRtfBox.Selection.End)
And then set the fontstyle:
rng.ApplyPropertyValue(Inline.FontSizeProperty, YourFontSizeValue)
rng.ApplyPropertyValue(Inline.FontFamilyProperty, YourFontFamilyValue)
要更改 RichTextBox 中所选内容的字体系列,您应该使用以下命令:
RichTextBox 中的所选文本是一个 Run 对象,因此必须使用 Run 依赖属性。
这似乎至少在 Silverlight 中有效,因此在 WPF 中也应该是同样的事情。
To change the font family for a selection in the RichTextBox you should use this:
The selected text in a RichTextBox is a Run object, so one must use the Run Dependency Properties.
This seems to work in Silverlight at least, so should be the same thing in WPF.