tags:

views:

23

answers:

1

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.

+1  A: 

You probably don't want

lastmousepoint = mousepos;

inside the foreach loop, because on subsequent iterations (mousepos.X - lastmousepoint.X) will always be zero, as will (mousepos.Y - lastmousepoint.Y). Adding 0 of course means no movement.

Jay
Genius. Sometimes, all you need is a second set of eyes.
DeadMG