移动折线WPF

时间:2015-01-13 17:56:40

标签: c# wpf wpf-controls

我想为我的所有程序移动一个加号,并获得符号中心的点。加号是一个形状,它在画布中:

<Canvas x:Name="canvas1" PreviewMouseLeftButtonDown="canvas1_PreviewMouseDown" 
        PreviewMouseMove="canvas1_PreviewMouseMove"
        PreviewMouseLeftButtonUp="canvas1_PreviewMouseUp" Grid.ColumnSpan="2" Margin="0,0,10,0">
        <Polyline Name="myPolyline"
          MouseDown="Polyline_MouseDown"
          Points="25,0 25,50 25,25 0,25 50,25" 
          Stroke="Blue"
          StrokeThickness="2"
          Height="50"/>
    </Canvas>

我的方法:

private void canvas1_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        //Check if the click was in a chape
        if (e.Source is Shape)
        {
            // Get the mouse position
            start = e.GetPosition(canvas1);
            // Initialize some components and set opacity to 50%
            isDragging = true;
            movedElement = (Shape)e.Source;
            ((Shape)e.Source).Opacity = 0.5;
            canvas1.CaptureMouse();
            e.Handled = true;
        }
    }

    private void canvas1_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        if (isDragging)
        {
            Point Pt = e.GetPosition(canvas1);
            // Get the actual position of the Shape
            double CurrentLeft = (double)movedElement.GetValue(Canvas.LeftProperty);
            double CurrentTop = (double)movedElement.GetValue(Canvas.TopProperty);

            // Calc the new position
            double newLeft = CurrentLeft + Pt.X - start.X;
            double newTop = CurrentTop + Pt.Y - start.Y;

            // Move the element
            movedElement.SetValue(Canvas.LeftProperty, newLeft);
            movedElement.SetValue(Canvas.TopProperty, newTop);

            start = Pt;
            e.Handled = true;
        }
    }

    private void canvas1_PreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
        // Restore the values
        movedElement.Opacity = 1;
        movedElement.SetValue(Canvas.ZIndexProperty, ++currentZ);
        isDragging = false;
        canvas1.ReleaseMouseCapture();
    }

我找到了一些完美移动线条,矩形和椭圆的方法,但是我的加号是折线,并且方法没有移动它,我相信共振是在线:

movedElement = (Shape)e.Source;

double CurrentLeft = (double)movedElement.GetValue(Canvas.LeftProperty); double CurrentTop = (double)movedElement.GetValue(Canvas.TopProperty);

因为GetValue正在返回“NaN”并且我真的不知道为什么。我试图使用两条线来制作加号,但我很难将这两条线移到聚醚上。任何人都可以告诉我是否存在代码中的错误或问题是我的加号的类型? 感谢。

2 个答案:

答案 0 :(得分:0)

您需要在XAML中初始化Canvas.Left和Canvas.Top属性,然后才能在代码隐藏中查询它们。它们默认为null。例如:

<Polyline Name="myPolyline"
    ...
    Canvas.Left="0"
    Canvas.Top="0"
    />

答案 1 :(得分:0)

强调文字

enter code here
    private void canvas2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        //create polyline
        polyline1 = new Polyline();
        polyline1.StrokeThickness = 4;
        polyline1.Stroke = Brushes.Black;
        Canvas.SetTop(polyline1, 0);
        Canvas.SetLeft(polyline1, 0);
    }


    private bool isPolyLine1Drog;
    double mousePosOnPolyline1X;
    double mousePosOnPolyline1Y;
    private void Polyline1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        isPolyLine1Drog = true;
        //get distance between current mouse position and top_left polyline
        mousePosOnPolyline1X= e.GetPosition(canvas2).X-Canvas.GetLeft(polyline1);
        mousePosOnPolyline1Y = e.GetPosition(canvas2).Y-Canvas.GetTop(polyline1);
        polyline1.CaptureMouse();
    }
    private void Polyline1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        isPolyLine1Drog = false;
        polyline1.ReleaseMouseCapture();
    }
    private void Polyline1_MouseMove(object sender, MouseEventArgs e)
    {
        if (!isPolyLine1Drog) return;
        //top=top+Y changing and left=left+X changing
        double left= e.GetPosition(canvas2).X - mousePosOnPolyline1X;
        double top = e.GetPosition(canvas2).Y - mousePosOnPolyline1Y;
        Canvas.SetLeft(polyline1, left);
        Canvas.SetTop(polyline1, top);
    }//mohsen_seif@yahoo.com