为什么 canvas 上的所有 UI 元素都会旋转,即使我只是尝试旋转选定的一个
Why all UI elements on the canvas gets rotated even i just try to rotate selected one
我有一个 canvas,里面有几张图片。我已附上 属性 每当我在鼠标上单击鼠标右键时,所选图像都会旋转 90 度。
它非常适合第一次轮换。
问题是当我再次单击时它不会再次旋转 90 度。
我的代码在这里:
sourceElt.MouseRightButtonUp += (sender, e) =>
{
RotateTransform myRotateTransform = new RotateTransform();
DragSourceBase advisorDraggable = GetDragSource(sender as DependencyObject);
if (advisorDraggable.IsDraggable(e.Source as UIElement) == false) return;
sourceElt = e.Source as UIElement;
sourceElt.RenderTransformOrigin = new Point(0.5, 0.5);
myRotateTransform.Angle = 90;
sourceElt.RenderTransform = myRotateTransform;
sourceElt.UpdateLayout();
_isMouseDown = false;
ifDraggedAfterRotation = true;
_offsetPoint = new Point(0, 0);
Mouse.Capture(null);
_draggedElt = null;
e.Handled = true;
}
为了解决之前的问题,我尝试像这样更改我的代码:
RotateTransform myRotateTransform = new RotateTransform(); //kept it outside rigth button click event
sourceElt.MouseRightButtonUp += (sender, e) =>
{
DragSourceBase advisorDraggable = GetDragSource(sender as DependencyObject);
if (advisorDraggable.IsDraggable(e.Source as UIElement) == false) return;
sourceElt = e.Source as UIElement;
sourceElt.RenderTransformOrigin = new Point(0.5, 0.5);
myRotateTransform.Angle += 90; // made it +=
sourceElt.RenderTransform = myRotateTransform;
sourceElt.UpdateLayout();
_isMouseDown = false;
ifDraggedAfterRotation = true;
_offsetPoint = new Point(0, 0);
Mouse.Capture(null);
_draggedElt = null;
e.Handled = true;
}
它解决了我之前的问题,但另一个问题来了,假设我旋转了一张图片,然后在我旋转了另一张图片后,问题是即使我没有选择之前旋转的图片,我也只是右键单击当前选定的图像,但它会同时旋转图像、所有先前选定的图像和当前选定的图像。
** 如何仅在 canvas 上旋转所选图像。**
您可能希望将现有 RotateTransform 的 Angle
属性 增加 90 度。如果元素的 RenderTransform
属性 尚未包含 RotateTransform,请创建并分配它。
var element = (UIElement)sender;
var rotateTransform = element.RenderTransform as RotateTransform;
if (rotateTransform == null)
{
rotateTransform = new RotateTransform();
element.RenderTransform = rotateTransform;
element.RenderTransformOrigin = new Point(0.5, 0.5);
}
rotateTransform.Angle += 90;
我有一个 canvas,里面有几张图片。我已附上 属性 每当我在鼠标上单击鼠标右键时,所选图像都会旋转 90 度。 它非常适合第一次轮换。 问题是当我再次单击时它不会再次旋转 90 度。
我的代码在这里:
sourceElt.MouseRightButtonUp += (sender, e) =>
{
RotateTransform myRotateTransform = new RotateTransform();
DragSourceBase advisorDraggable = GetDragSource(sender as DependencyObject);
if (advisorDraggable.IsDraggable(e.Source as UIElement) == false) return;
sourceElt = e.Source as UIElement;
sourceElt.RenderTransformOrigin = new Point(0.5, 0.5);
myRotateTransform.Angle = 90;
sourceElt.RenderTransform = myRotateTransform;
sourceElt.UpdateLayout();
_isMouseDown = false;
ifDraggedAfterRotation = true;
_offsetPoint = new Point(0, 0);
Mouse.Capture(null);
_draggedElt = null;
e.Handled = true;
}
为了解决之前的问题,我尝试像这样更改我的代码:
RotateTransform myRotateTransform = new RotateTransform(); //kept it outside rigth button click event
sourceElt.MouseRightButtonUp += (sender, e) =>
{
DragSourceBase advisorDraggable = GetDragSource(sender as DependencyObject);
if (advisorDraggable.IsDraggable(e.Source as UIElement) == false) return;
sourceElt = e.Source as UIElement;
sourceElt.RenderTransformOrigin = new Point(0.5, 0.5);
myRotateTransform.Angle += 90; // made it +=
sourceElt.RenderTransform = myRotateTransform;
sourceElt.UpdateLayout();
_isMouseDown = false;
ifDraggedAfterRotation = true;
_offsetPoint = new Point(0, 0);
Mouse.Capture(null);
_draggedElt = null;
e.Handled = true;
}
它解决了我之前的问题,但另一个问题来了,假设我旋转了一张图片,然后在我旋转了另一张图片后,问题是即使我没有选择之前旋转的图片,我也只是右键单击当前选定的图像,但它会同时旋转图像、所有先前选定的图像和当前选定的图像。
** 如何仅在 canvas 上旋转所选图像。**
您可能希望将现有 RotateTransform 的 Angle
属性 增加 90 度。如果元素的 RenderTransform
属性 尚未包含 RotateTransform,请创建并分配它。
var element = (UIElement)sender;
var rotateTransform = element.RenderTransform as RotateTransform;
if (rotateTransform == null)
{
rotateTransform = new RotateTransform();
element.RenderTransform = rotateTransform;
element.RenderTransformOrigin = new Point(0.5, 0.5);
}
rotateTransform.Angle += 90;