我有一个画布在scrollviewer
. 此画布是一个动态画布,可以在其上放置许多其他控件。当控件被拖出画布区域(即右侧和底部)时,将显示滚动查看器。
我已经实现了放大和缩小画布的代码,效果很好,但找不到代码来实现使动态画布内容适合实际画布大小。
我正在寻找能够自动使画布内容适合其大小的东西,即计算缩放比例以使内容适合画布大小。
我有一个画布在scrollviewer
. 此画布是一个动态画布,可以在其上放置许多其他控件。当控件被拖出画布区域(即右侧和底部)时,将显示滚动查看器。
我已经实现了放大和缩小画布的代码,效果很好,但找不到代码来实现使动态画布内容适合实际画布大小。
我正在寻找能够自动使画布内容适合其大小的东西,即计算缩放比例以使内容适合画布大小。
This is a complicated one. You should work with some properties of the ScrollViewer to get the desired result: ContentHorizontalOffset, ContentVerticalOffset, ViewportWidth, ViewportHeight (among others existing in the component..)
Here is a snippet of code that helped me:
Point realSize = new Point(this.canvasAreas.ActualHeight, this.canvasAreas.ActualWidth);
Point sizeAvailable = new Point(this.scroolMain.ActualHeight, this.scroolMain.ActualWidth);
double scaleX = sizeAvailable.X / realSize.X;
double scaleY = sizeAvailable.Y / realSize.Y;
double newScale = Math.Round(Math.Min(scaleX, scaleY), 2);
this.gridScaleZoom.ScaleX = newScale;
this.gridScaleZoom.ScaleY = newScale;
There are several articles on CodeProject and looking on google that can assist you in working with ScrollViewer zoom. Although most are complicated and complex. Hope this helps you a bit.
这是我的 xaml 代码:
并在按钮上单击我想相应地缩放。我已经尝试了下面的代码。
Point realSize = new Point(this.formCanvas.ActualHeight, this.formCanvas.ActualWidth);
Point sizeAvailable = new Point(this.myScrollViewer.ActualHeight, this.myScrollViewer.ActualWidth);
double scaleX = sizeAvailable.X / realSize.X;
double scaleY = sizeAvailable.Y / realSize.Y;
double newScale = Math.Round(Math.Min(scaleX, scaleY), 2);
this.canvasScale.ScaleX = newScale;
this.canvasScale.ScaleY = newScale;
结果也是一样的。如果代码不正确,请更正代码。