这是我在XAML中的画布:
<TabItem Name="tabPlanView" Header=" Graphics " Background="White">
<Border x:Name="CanvasBorder" BorderBrush="Black" BorderThickness="1" Margin="-2,-1,-2,1" ClipToBounds="True">
<Grid>
<Canvas Name="mainCanvas" ClipToBounds="True" Background="#FF061D30" MouseWheel="mainCanvas_MouseWheel" MouseDown="canvasDrawing_MouseDown" MouseMove="canvasDrawing_MouseMove" SnapsToDevicePixels="True" MouseLeave="mainCanvas_MouseLeave" >
<Canvas.LayoutTransform>
<ScaleTransform x:Name="st" ScaleX="1" ScaleY="-1" CenterX=".5" CenterY=".5" />
</Canvas.LayoutTransform>
<Canvas.RenderTransform>
<MatrixTransform/>
</Canvas.RenderTransform>
</Canvas>
<Image x:Name="img_Axis" MouseMove="canvasDrawing_MouseMove" Height="100" Source="pack://application:,,,/images/axisss.gif" Margin="10,0,0,10" HorizontalAlignment="Left" VerticalAlignment="Bottom"/>
</Grid>
</Border>
</TabItem>
以下是代码背后的MouseWheel事件:
var element = sender as UIElement;
var position = e.GetPosition(element);
var transform = element.RenderTransform as MatrixTransform;
var matrix = transform.Matrix;
var scale = e.Delta >= 0 ? 1.1 : (1.0 / 1.1);
matrix.ScaleAtPrepend(scale, scale, position.X, position.Y);
transform.Matrix = matrix;
在下图中,当我缩小画布边界及其父(CanvasBorder)边界不匹配时。
我尝试了LayoutTransform。它可以工作,但它无法灵活地进行相对于鼠标位置的缩放(只有一些与左下角相关的固定缩放)。
我需要一个可缩放的画布,其边框有一定的尺寸,并且与鼠标位置相关的灵活轮缩放。