Is it possible to “zoom” the text in a WPF RichTextBox?

♀尐吖头ヾ 提交于 2019-12-20 15:27:20

问题


I noticed the WinForms RichTextBox has a ZoomFactor property that I assume is exactly what I want--unfortunately this seems to be entirely missing on the WPF variant.

Is there any way I can achieve the same functionality (increasing/decreasing the visible text size of the whole document without actually changing the underlying RTF)?

Update: While setting a LayoutTransform on the RichTextBox does seem to work under very simple settings, it's not exactly the same as setting ZoomFactor because of a couple things:

  • First, the scroll bar is zoomed also. This just looks silly.
  • Second, in my app (for some reason, but not in Kaxaml--I'll explore this to figure out why), the text is bitmap zoomed so that it just enlarges the rendered text as opposed to vector-zooming it so it's smooth. Here's an example of what I'm talking about (note the way-big custom scroll bar):

Update 2: Okay I discovered that the bitmap zooming was being caused by setting TextOptions.TextFormattingMode to Display instead of Ideal. Setting it to ideal reintroduces vector zooming.

However there is still that pesky scroll bar! I mean one option is to disable scrolling on the RichTextBox and wrap it in a ScrollViewer, but I wonder if that would deteriorate performance. I also wonder if text wrapping would still work if I did that.


回答1:


This should get you started:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <DockPanel LastChildFill="True">  
     <Slider x:Name="Scale" DockPanel.Dock="Bottom" Minimum="1" Maximum="20"/>
     <RichTextBox>
      <RichTextBox.LayoutTransform>
        <ScaleTransform ScaleX="{Binding ElementName=Scale, Path=Value}" ScaleY="{Binding ElementName=Scale, Path=Value}"/>
      </RichTextBox.LayoutTransform>
     </RichTextBox>
  </DockPanel>
</P



回答2:


I noticed the WinForms RichTextBox has a ZoomFactor property that I assume is exactly what I want--unfortunately this seems to be entirely missing on the WPF variant.

You need to get back and read the basics of WPF. Item by Item. Stop at TRANSFORMS. The reason that a ZoomFactor is missing in the TextBox is that EVERY WPF CONTROL can be TRANSFORMED (zoom, 3d transforms) and ANIMATED by generic standard measures - so a special approach is simply unneeded.




回答3:


Take a look at the FlowDocumentReader as I think that has what you are looking for.



来源:https://stackoverflow.com/questions/3430629/is-it-possible-to-zoom-the-text-in-a-wpf-richtextbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!