2

I have an image in a Canvas, but when I use DoubleAnimation to move it and then use Canvas.SetLeft(image), the method is ignored, the only way to move it again is with animations. What's wrong with Canvas.SetLeft(control)?

Here is an explanation with images of what's happening:

enter image description here

The default coin position is 0 in Canvas.Left property.

enter image description here

I move it with the mouse with the next code;

Point p = Mouse.GetPosition(cCoinContainer);
Canvas.SetLeft(iCoin, p.X);

enter image description here

And when the mouse left the Canvas, the animation moves the coin to it's original Canvas.Left position.

The problem is when I do it again (move the coin with the mouse with Canvas.SetLeft(iCoin, p.X); it's completely ignored, only works with animations.

AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38
FukYouAll
  • 111
  • 1
  • 3
  • 15

2 Answers2

2

Your problem seems to be one of general issues addressed in MSDN post about Animation Tips and Tricks. To solve the problem, try to set animation's FillBehavior to Stop :

<Storyboard>
    <DoubleAnimation 
        FillBehavior="Stop"
        ......
        />
</Storyboard>

By setting FillBehavior to Stop, you tell the animation to stop affecting its target property after it reaches the end of its active period. [MSDN]

har07
  • 88,338
  • 12
  • 84
  • 137
0

i think you want to make the image draggable with the mouse? if am right; you should add a MouseCapture() on the rectangle to update the mouse position with mouse animation and call RealeaseMouseCapture when you finish; at MouseUp Event cause the GetMousePosition return the current mouse position fore once. good luck. if you need some, feel free to ask!

Henka Programmer
  • 433
  • 6
  • 18