自动缩放WPF TextBox / RichTextBox内容以适应可用空间

时间:2011-11-24 11:29:59

标签: wpf richtextbox scaling layouttransform

我有以下问题:

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

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

到目前为止我所建立的是RichTextBox模板的AdornerDecorator中的LayoutTransform,它包含在ScrollViewer中,以便我可以触发代码隐藏方法来计算Scaling。

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

只要没有TextWrapping(这样X和Y的缩放不会导致Text在不同位置换行并另外改变高度),这种方法效果很好。 有什么想法吗?

我创建了一个小型演示,使事情更加清晰:

<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

0 个答案:

没有答案