TranslateTransform 不翻译行

TranslateTransform Not translating lines

我一直在尝试通过 TranslateTransform 在 canvas 中翻译网格(2 行数组)。但是,这些行只是摇晃 return 到它们原来的位置,而不是翻译。这是来源:

    private Point mousePosition;
    private bool CanvasMouseDown;

    private void GraphMouseDown(object sender, MouseButtonEventArgs e)
    {
        CanvasMouseDown = true;
    }

    private void GraphMouseMove(object sender, MouseEventArgs e)
    {
        if (CanvasMouseDown)
        {
            var position = e.GetPosition(canvas);
            var offset = position - mousePosition;
            mousePosition = position;

            for (int i = 0; i < GridLinesHoriz.Length; ++i)
            {
                GridLinesHoriz[i].RenderTransform = new TranslateTransform(offset.X, offset.Y);
            }

            for (int i = 0; i < GridLinesVert.Length; ++i)
            {
                GridLinesVert[i].RenderTransform = new TranslateTransform(offset.X, offset.Y);
            }
        }
    }


    private void GraphMouseUp(object sender, MouseButtonEventArgs e)
    {
        CanvasMouseDown = false;
    }

有帮助吗?

问题是您在处理 MouseMove 事件时正在标准化偏移量。 TranslateTransform 始终从 原始 位置转换,因此您需要计算一个 运行 偏移量,该偏移量在拖动操作的生​​命周期内保持不变。

如果您将mousePosition 更改为class 级别变量并在操作开始时设置该变量(在MouseDown 事件处理程序中)并在[=11 中停止更新该变量=] 事件处理程序,这些行将按照您期望的方式开始翻译。

point mousePosition;

private void GraphMouseDown(object sender, MouseButtonEventArgs e)
{
    CanvasMouseDown = true;
    mousePosition  = e.GetPosition(canvas);
}

private void GraphMouseMove(object sender, MouseEventArgs e)
{
    if (CanvasMouseDown)
    {
        var position = e.GetPosition(canvas);
        var offset = position - mousePosition;

        for (int i = 0; i < GridLinesHoriz.Length; ++i)
        {
            GridLinesHoriz[i].RenderTransform = new TranslateTransform(offset.X, offset.Y);
        }

        for (int i = 0; i < GridLinesVert.Length; ++i)
        {
            GridLinesVert[i].RenderTransform = new TranslateTransform(offset.X, offset.Y);
        }
    }
}