【发布时间】:2019-03-12 04:16:55
【问题描述】:
我创建了一个包含一些文本框、图像等的用户控件。现在我想用鼠标旋转该用户控件。我已经成功旋转,但是在旋转过程中它倒转,我在哪里做错了?下面是我的 xaml 和代码:
<UserControl...
<Grid x:Name="ContainerGrid"
Width="300" Height="400" RenderTransformOrigin="0.5,0.5"
ManipulationMode="TranslateX,TranslateY,Rotate"
ManipulationStarted="Manipulator_OnManipulationStarted"
ManipulationDelta="Manipulator_OnManipulationDelta">
<Grid.RenderTransform>
<RotateTransform x:Name="RotateGrid" Angle="0"/>
</Grid.RenderTransform>
<!-- some elements -->
<Rectangle x:Name="RotateRectangle"
IsHitTestVisible="False"
Width="16" Height="16"
Fill="Red"
VerticalAlignment="Top"
HorizontalAlignment="Right" />
</Grid>
private void Manipulator_OnManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
if (e.Position.X > ContainerGrid.Width - RotateRectangle.Width &&
e.Position.Y < ContainerGrid.Height - RotateRectangle.Height)
{
_isRotating = true;
lastPosition = e.Position;
return;
}
_isRotating = false;
}
private void Manipulator_OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
if (_isRotating)
{
Point currentLocation = e.Position;
double radians = Math.Atan2((currentLocation.Y - lastPosition.Y), (currentLocation.X - lastPosition.X));
var angle = radians * 180 / Math.PI;
RotateGrid.Angle = angle;
lastPosition = currentLocation;
}
}
【问题讨论】:
-
在 Blend 中执行此操作不是更容易/更清洁/更顺畅吗?
-
我没有使用情节提要来旋转这个元素,正如你所说的那样更容易。但是,我希望使用鼠标为用户提供旋转功能,以这种方式实现。用户必须使用鼠标旋转元素。