1

我有以下问题:

我想设置一个 RichTextBox,它可以自动调整内容的大小以适应最大的可用空间,同时不改变内容的布局(例如字体大小、缩进等)。

我已经看到很多关于缩放 TextBox 内容的问题,但所有问题都与某种缩放滑块有关。我想要的是真正计算缩放比例,以便它自动最适合文本框。

到目前为止,我在 RichTextBox 模板的 AdornerDecorator 中建立了一个 LayoutTransform,该模板包含在 ScrollViewer 中,以便我可以触发代码隐藏方法来计算缩放。

最初,当所有内容都适合 ViewPort 时,RichTextBox 不应缩放。只要需要启用垂直 ScrollBar,我就会更改 ScaleFactor。

只要不涉及 TextWrapping 就可以很好地工作(因此 X 和 Y 的缩放不会导致文本在不同位置换行并另外改变高度)。有任何想法吗?

我创建了一个小演示,让事情更清楚一点:

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
    <ScaleTransform x:Key="ScaleTransform" ScaleY="{Binding ScaleFactor}"
                                           ScaleX="{Binding ScaleFactor}"/>
</Window.Resources>
<Grid>
    <RichTextBox AcceptsTab="True" 
                 Background="Transparent" 
                 BorderBrush="Transparent" 
                 VerticalScrollBarVisibility="Auto"
                 HorizontalScrollBarVisibility="Disabled"
                 HorizontalAlignment="Stretch" 
                 VerticalAlignment="Stretch">
        <RichTextBox.Template>
            <ControlTemplate TargetType="{x:Type TextBoxBase}">
                <Border CornerRadius="2" 
                        Background="{TemplateBinding Background}" 
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        BorderBrush="{TemplateBinding BorderBrush}">
                    <ScrollViewer ScrollChanged="ScrollViewer_ScrollChanged">
                        <AdornerDecorator x:Name="PART_ContentHost" Focusable="False"
                                          LayoutTransform="{StaticResource ScaleTransform}"/>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </RichTextBox.Template>
    </RichTextBox>
</Grid>

和代码隐藏:

public partial class Window1 : Window, INotifyPropertyChanged
{
    public Window1()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    private double scaleFactor = 1.0;
    public double ScaleFactor
    {
        get { return scaleFactor; }
        set
        {
            scaleFactor = value;
            OnPropertyChanged("ScaleFactor");
        }
    }

    private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
    {
        if (e.ExtentHeightChange == 0)
            return;

        if (e.Source.GetType() == typeof(RichTextBox))
            return;

        var missingHeight = e.ExtentHeightChange;
        var heightWithoutScaling = e.ExtentHeight / ScaleFactor;

        if (e.ViewportHeight <= heightWithoutScaling)
        {
            ScaleFactor = ((e.ViewportHeight / heightWithoutScaling));
        }

    }

    #region INotifyPropertyChanged Members

    /// <summary>
    /// Raised when a property on this object has a new value
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Raises this object's PropertyChanged event.
    /// </summary>
    /// <param name="propertyName">The property that has a new value.</param>
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }

    #endregion
4

0 回答 0