ScrollViewer如何缩放滚动?

时间:2018-08-01 17:02:17

标签: c# wpf scrollviewer

在创建某种图像处理程序时,我遇到了缩放问题。我正在使用放在ScrollViewer中作为表面的画布。

<ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
    <Canvas Height="{Binding GridHeight}" Width="{Binding GridWidth}">
        <Canvas.RenderTransform>
            <ScaleTransform  ScaleX="{Binding ScaleX}" ScaleY="{Binding ScaleY}"/>
        </Canvas.RenderTransform>
    </Canvas>               
</ScrollViewer>

当我的应用程序以窗口模式启动时,它看起来像这样:

enter image description here 但是在将其调整为全窗口模式后,滚动条消失了,但是表面的比例保持不变:

enter image description here 就像我想要的那样工作。但是我不希望滚动条消失。 当我更换画布的ScaleXScaleY时,即使表面的填充量超过整个ScrollViewer,滚动条也不会重新出现。

是否可以通过编程方式或其他解决方案来更改ScrollViewer对象的ScrollScale?

2 个答案:

答案 0 :(得分:1)

问题是您的ScrollViewer不了解ScaleTransform对Canvas所做的更改。我为解决此问题所做的事情是添加了Grid控件,该控件将托管Canvas控件,并且我将Height的{​​{1}}和Width设置为{{ 1}}和Grid分别以这种方式为您分配了特定尺寸的控件,CanvasHeight * ScaleY会注意到它。

我已经创建了一个演示应用程序,它将帮助您理解问题,顺便说一句,我在此处发布的代码正在按您期望的方式工作:

ValueConverter.cs

CanvasWidth * ScaleX

ViewModel.cs

ScrollViewer

MainWindow.xaml.cs

public class ValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double originalValue = System.Convert.ToDouble(value);
        double multiplier = System.Convert.ToDouble(parameter);

        return originalValue * multiplier;
    }

    public object ConvertBack(object value, Type targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

MainWindow.xaml

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private double gridHeight = 500.0;
    public double GridHeight
    {
        get { return gridHeight; }
        private set { gridHeight = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(GridHeight))); }
    }


    private double gridWidth = 500.0;
    public double GridWidth
    {
        get { return gridWidth; }
        private set { gridWidth = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(GridWidth))); }
    }


    private int scaleX = 1;
    public int ScaleX
    {
        get { return scaleX; }
        set
        {
            if (value > 0)
            {
                var oldScaleX = scaleX;
                scaleX = value;
                GridWidth *= (double)scaleX / oldScaleX;
            }

            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ScaleX)));
        }
    }


    private int scaleY = 1;
    public int ScaleY
    {
        get { return scaleY; }
        set
        {
            if (value > 0)
            {
                var oldScaleY = scaleY;
                scaleY = value;
                GridHeight *= (double)scaleY / oldScaleY;
            }

            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ScaleY)));
        }
    }
}

答案 1 :(得分:-1)