我正在遍历给定Canvas对象的所有子节点。麻烦的是,它只影响了两个孩子中的一个。我在另一个类似事件中的另一次迭代工作得很好。我有这个,所以你可以拖动元素:
private Point lastmousepoint;
private void Window_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
{
Point mousepos = e.GetPosition(this);
foreach (UIElement element in canvas1.Children)
{
Canvas.SetLeft(element, Canvas.GetLeft(element) + (mousepos.X - lastmousepoint.X));
Canvas.SetTop(element, Canvas.GetTop(element) + (mousepos.Y - lastmousepoint.Y));
lastmousepoint = mousepos;
}
}
e.Handled = true;
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
lastmousepoint = e.GetPosition(this);
e.Handled = true;
}
private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
但是这两个文本中只有一个是移动的,而它们都应该移动。实际运动很好并按预期工作。
此代码
private int CurrentScaleLevel = 0;
private void Window_MouseWheel(object sender, MouseWheelEventArgs e)
{
foreach (UIElement element in canvas1.Children)
{
Point p = e.MouseDevice.GetPosition(element);
Matrix m = element.RenderTransform.Value;
if (e.Delta > 0)
{
CurrentScaleLevel++;
m.ScaleAtPrepend(1.1, 1.1, p.X, p.Y);
}
else
{
CurrentScaleLevel--;
m.ScaleAtPrepend(1 / 1.1, 1 / 1.1, p.X, p.Y);
}
Canvas.SetLeft(element, Canvas.GetLeft(element) + m.OffsetX);
Canvas.SetTop(element, Canvas.GetTop(element) + m.OffsetY);
m.Translate(-m.OffsetX, -m.OffsetY);
element.RenderTransform = new MatrixTransform(m);
}
e.Handled = true;
}
影响两个对象就好了,就像它应该的那样。
答案 0 :(得分:1)
你可能不想要
lastmousepoint = mousepos;
在foreach
循环中,因为在后续迭代(mousepos.X - lastmousepoint.X)
将始终为零,(mousepos.Y - lastmousepoint.Y)
也是如此。添加0当然意味着没有动作。