我一直试图通过TranslateTransform在画布中翻译网格(2个线阵列)。然而,线条只是摇晃并回到原来的地方而不翻译。这是来源:
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;
}
任何帮助?
答案 0 :(得分:1)
问题是您在处理MouseMove
事件时正在规范偏移量。 TranslateTransform
始终从原始位置进行转换,因此您需要计算在拖动操作的生命周期内维护的运行偏移量。
如果将mousePosition
更改为类级变量并在操作开始时设置该变量(在MouseDown
事件处理程序中)并停止在MouseMove
事件处理程序中更新该变量,行将开始翻译你期望的方式。
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);
}
}
}