Animating grid cells
I want to make an animation where one object would move from one grid cell to another. I tried calculating the positions of the objects but that didn't work (it gave me wrong values). Here's the code if anyone interested:
Point locationToScreen = (((e.Source as Button).Content as Viewbox).Child as Label).PointToScreen(new Point(0, 0));
PresentationSource source = PresentationSource.FromVisual((((e.Source as Button).Content as Viewbox).Child as Label));
Point pt = source.CompositionTarget.TransformFromDevice.Transform(locationToScreen);
Point a = (letters.Children[_words开发者_StackOverflow中文版[_index].Index] as TextBlock).PointToScreen(new Point(0, 0));
PresentationSource sos = PresentationSource.FromVisual((letters.Children[_words[_index].Index] as TextBlock));
Point pt2 = sos.CompositionTarget.TransformFromDevice.Transform(a);
TranslateTransform tt = new TranslateTransform();
(((e.Source as Button).Content as Viewbox).Child as Label).RenderTransform = tt;
DoubleAnimation da1 = new DoubleAnimation(0, -(pt.Y - pt2.Y)/2, new Duration(TimeSpan.FromSeconds(1)));
DoubleAnimation da2 = new DoubleAnimation(0, -(pt.X - pt2.X)/2, new Duration(TimeSpan.FromSeconds(1)));
tt.BeginAnimation(TranslateTransform.YProperty, da1);
tt.BeginAnimation(TranslateTransform.XProperty, da2);
So how should I do this? Is there any simple way to move object from one container to another?
If you do not need animate kind of 1000 objects simultaneously it definitely better that you create a new UI element which appears on top of Source
element and moves till Destination
element, after animation finished that element disappears and Source
and Destination
elements properties changes as requested.
I personally think that it's easier to use XAML, to me it seems more clear . Here is an example of use: Animation XAML
The basic idea is, declare animation under your TextBlock
tag and bind it to some dependency property that change of which will launch an animation in desired moment.
精彩评论