1

我有一个画布,画布上有不同数量的子元素。我创建了一个拖动和移动功能,但没有一个孩子在移动。

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。

彼得

4

3 回答 3

2

设置Canvas.Top、调用Canvas.SetTop(child, top)和类似的 for Left

请注意,它是一种静态方法 -Canvas.SetTop而不是this.canv.SetTop.

于 2012-07-26T13:04:23.383 回答
2

首先,使用它来获取位置:

双 deltaV = e.GetPosition(parentofcanvas).Y - pot.Y;
双 deltaH = e.GetPosition(parentofcanvas).X - pot.X;

其次,使用 Canvas.SetTop(..)、Canvas.SetLeft(..),但如果您的 Canvas 不在 Canvas 中,则必须使用其 Margin 属性。

于 2012-07-26T13:07:24.943 回答
1

试试我的代码:XAML - 矩形

<Rectangle x:Name="Rectangle" MouseDown="rectangle1_MouseDown" MouseUp="Rectangle_MouseUp"
               Width="122" Height="78" Stretch="Fill"
               ClipToBounds="False" Canvas.Left="19" Canvas.Top="19">
                <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>

C#:

 public MainWindow()
    {
        InitializeComponent();
        DispatcherTimer dt = new DispatcherTimer();
        dt.Tick += new EventHandler(dt_Tick);
        dt.Start();
    }

    void dt_Tick(object sender, EventArgs e)
    {
        if (pressed == true)
        {
            Point c = Mouse.GetPosition(canvas1);
            Point c1 = Mouse.GetPosition(Rectangle);

            Canvas.SetLeft(Rectangle, c.X - c1.X);
            Canvas.SetTop(Rectangle, c.Y - c1.Y);
        }
    }
    bool pressed = false;
    private void rectangle1_MouseDown(object sender, MouseButtonEventArgs e)
    {
        pressed = true;
    }

    private void Rectangle_MouseUp(object sender, MouseButtonEventArgs e)
    {
        pressed = false;
    }
于 2012-07-26T13:21:22.943 回答