在用故事板掠过画布之后,我不知道为什么我不能像我所做的那样移动画布。这是我的代码,请帮助我。
在xaml页面中:
<Canvas Name="pnEditor" Background="Transparent" MouseLeftButtonUp="pnEditor_MouseLeftButtonUp" ManipulationDelta="PnEditor_OnManipulationDelta" ManipulationStarted="PnEditor_OnManipulationStarted" RenderTransformOrigin="0.5,0.5" Margin="-10,10,10,-10">
<Border Name="adornerControl" BorderBrush="Green" BorderThickness="1" MouseLeftButtonDown="adornerControl_MouseLeftButtonDown">
<Border.RenderTransform>
<CompositeTransform x:Name="ControlRotation" Rotation="0"/>
</Border.RenderTransform>......
</Border>
</Canvas>
在代码背后: 的修改
private void PnEditor_OnManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
//I have a canvas with 5 holders
if (mIsHolderPressed)
{
double deltaX = e.DeltaManipulation.Translation.X;
double deltaY = e.DeltaManipulation.Translation.Y;
//center -> move
if (mSelectedHolder == holderCenter)
{
mCurrentX = Math.Max(0, mCurrentX + deltaX);
mCurrentY = Math.Max(0, mCurrentY + deltaY);
Canvas.SetLeft(adornerControl, mCurrentX);
Canvas.SetTop(adornerControl, mCurrentY);
}
...
//hold it to rotate the canvas follow the mouse
else if (mSelectedHolder == holderSouthWest)
{
var angel = (Math.Atan2(deltaY, deltaX)*180/Math.PI) - 45;
if (deltaX < 0) angel -= 180;
var currentAngel = ControlRotation.Rotation;
CompositeTransform ct = new CompositeTransform();
ct.TranslateX = Math.Max(0, mCurrentX + deltaX);
ct.TranslateY = Math.Max(0, mCurrentY + deltaY);
ct.Rotation = currentAngel + angel;
adornerControl.RenderTransform = ct;
adornerControl.RenderTransformOrigin = new Point(0.5, 0.5);
}
}
}
通过此方法获取adornerControl的当前X,Y:
private void PnEditor_OnManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
mOriginalPoint = e.ManipulationOrigin;
mCurrentX = Canvas.GetLeft(adornerControl);
mCurrentY = Canvas.GetTop(adornerControl);
}