在创建某种图像处理程序时,我遇到了缩放问题。我正在使用放在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>
当我的应用程序以窗口模式启动时,它看起来像这样:
但是在将其调整为全窗口模式后,滚动条消失了,但是表面的比例保持不变:
就像我想要的那样工作。但是我不希望滚动条消失。
当我更换画布的ScaleX
和ScaleY
时,即使表面的填充量超过整个ScrollViewer
,滚动条也不会重新出现。
是否可以通过编程方式或其他解决方案来更改ScrollViewer
对象的ScrollScale?
答案 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)
scrollviewer
具有HorizontalScrollBarVisibilityProperty
和VerticalScrollBarVisibilityProperty
您应该可以将它们强制在您的xaml中的visible
上始终显示。
请参阅:https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.scrollviewer.horizontalscrollbarvisibility?view=netframework-4.7.2#System_Windows_Controls_ScrollViewer_HorizontalScrollBarVisibility 和:https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.scrollviewer.verticalscrollbarvisibility?view=netframework-4.7.2#System_Windows_Controls_ScrollViewer_VerticalScrollBarVisibility
进一步阅读。