我有一个画布,画布上有不同数量的子元素。我创建了一个拖动和移动功能,但没有一个孩子在移动。
if (blockClicked == false && canvasClicked == true)
{
if (isDrag == true)
{
double deltaV = e.GetPosition(null).Y - pot.Y;
double deltaH = e.GetPosition(null).X - pot.X;
double newTop = deltaV + (double)canv.GetValue(Canvas.TopProperty);
double newLeft = deltaH + (double)canv.GetValue(Canvas.LeftProperty);
Console.WriteLine("newTop: " + newTop);
Console.WriteLine("newLeft: " + newLeft);
this.canv.SetValue(Canvas.TopProperty, newTop);
this.canv.SetValue(Canvas.LeftProperty, newLeft);
Console.WriteLine("canv new top: " + canv.GetValue(Canvas.TopProperty));
Console.WriteLine("canv new left: " + canv.GetValue(Canvas.LeftProperty));
}
}
上面是我正在使用的代码,外部 if 语句只是检查标志,但内部 IF 语句是我确定是拖动鼠标的地方。我得到鼠标点(锅),然后测量它的移动量并将画布值设置为该测量值。
但是画布上的子元素都没有移动吗?谁能向我解释为什么会这样?
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="canv"
ClipToBounds="False"
d:DesignHeight="0" d:DesignWidth="1180">
<Canvas.RenderTransform>
<ScaleTransform x:Name="scale"/>
</Canvas.RenderTransform>
<!--<Canvas x:Name="canv" Width="1000" Height="750" Canvas.Left="0" Canvas.Top="0">-->
<Rectangle x:Name="Rectangle"
Width="1180" Height="784" Stretch="Fill"
ClipToBounds="False">
<Rectangle.Fill>
<RadialGradientBrush RadiusX="0.763909" RadiusY="0.611915" Center="0.496313,0.50023" GradientOrigin="0.496313,0.50023">
<RadialGradientBrush.GradientStops>
<GradientStop Color="#00456487" Offset="0.489691" />
<GradientStop Color="#B3456487" Offset="0.90404" />
</RadialGradientBrush.GradientStops>
<RadialGradientBrush.RelativeTransform>
<TransformGroup />
</RadialGradientBrush.RelativeTransform>
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Canvas>
编辑:以上是“canv”画布包含的 XAML。
彼得