使用鼠标移动事件更新坐标时,WPF中的直线(十字准线)不会更新- C#

我正在尝试在WPF画布上创建两条线的十字线。

我发现,当我初始化线条时,它们是正确绘制的。但是当我使用鼠标移动事件更新线的坐标时,线没有更新。

当‘`currentMousePoint’点的坐标更新时,为什么线不更新?

public CADControl()
    {
        InitializeComponent();
    }
    
    //(500, 500) added just to test the lines appear which they do.
    private Point currentMousePoint = new Point(500,500);

    private void CadCanvas_MouseMove(object sender, MouseEventArgs e)
    {
        currentMousePoint = e.GetPosition(this);
        //coordinates appear to update correctly in the console
        Console.WriteLine("x: " + currentMousePoint.X + "  y: " + currentMousePoint.Y);
        InvalidateVisual();
        UpdateLayout();
    }

    private double screenWidth;
    private double screenHeight;

    private void DrawCursorCrosshair()
    {
        screenWidth = CadCanvas.ActualWidth;
        screenHeight = CadCanvas.ActualHeight;

        Line horizontalLine = new Line();
        horizontalLine.Stroke = Brushes.White;
        horizontalLine.X1 = 0;
        horizontalLine.X2 = screenWidth;
        horizontalLine.Y1 = currentMousePoint.X;
        horizontalLine.Y2 = currentMousePoint.X;

        CadCanvas.Children.Add(horizontalLine);

        Line verticalLine = new Line();
        verticalLine.Stroke = Brushes.White;
        verticalLine.X1 = currentMousePoint.X;
        verticalLine.X2 = currentMousePoint.X;
        verticalLine.Y1 = 0;
        verticalLine.Y2 = screenHeight;

        CadCanvas.Children.Add(verticalLine);

    }

    private void CadCanvas_Loaded(object sender, RoutedEventArgs e)
    {
        DrawCursorCrosshair();
    }

转载请注明出处:http://www.fdzxgo.com/article/20230526/2013787.html