I'm iterating through all the children of a given Canvas object. The trouble is, it's only impacting one of the two children. The other iteration I have in another similar event is working just fine. I've got this so that you can drag the elements:
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;
}
But only one of the two texts is moving, whereas they should both move. The actual movement is fine and working as intended.
This code
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;
}
Affects both the objects just fine, just like it should.