为什么画布的子元素不会移动?

时间:2012-07-26 13:01:14

标签: c# wpf xaml canvas

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

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”Canvas中包含的XAML。

彼得

3 个答案:

答案 0 :(得分:2)

要设置Canvas.Top,请致电Canvas.SetTop(child, top),同样请Left

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

答案 1 :(得分:2)

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

  

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

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

答案 2 :(得分: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;
    }