I have a canvas where I move some cards around. Everything happens manually in code behind. In the sizeChanged event I move the cards to accommodate to the new canvas size. When one item is clicked I maximize it (using animations), and when user clicks on the exit button I revert the animation.
After that, when I change the size again, the Canvas.SetRight on the animated element DOES NOT AFFECT it. Is amazing. Whatever item I maximize and make small again, won't adapt to the changes in the position whereas the other ones will.
I also noted that if I delete all the viewers when minimizing, and create new ones. The new ones will actually move correctly. So it is definitely the animation blocking the Canvas.SetRight somehow
This is how I update the position of the items on size change:
foreach (PatientViewer pv in Viewers.OrderBy(x => x.PatientName))
{
pv.Width = PVWidth;
pv.Height = PVHeight;
PatientContainerCanvas.Children.Add(pv);
Point position = PositionCalculator();
Canvas.SetRight(pv, position.X);
Canvas.SetTop(pv, position.Y);
Canvas.SetZIndex(pv, 1);
pv.Cursor = Cursors.Hand;
}
Then one PatientViewer get's clicked, it goes full screen. I set bigger Z-index and animate its size and its position, while keeping track of he original position, to be able to revert the animation. Like this:
PatientViewer pv = sender as PatientViewer;
initialX = Canvas.GetRight(pv); //class variable
initialY = Canvas.GetTop(pv); //class variable
Canvas.SetZIndex(pv, 10);
DoubleAnimation Xtozero = new DoubleAnimation(initialX, 0, animationTime);
DoubleAnimation YtoZero = new DoubleAnimation(initialY, 0, animationTime);
DoubleAnimation WidthToFull = new DoubleAnimation(MainGrid.ActualWidth, animationTime);
DoubleAnimation HeightTofull = new DoubleAnimation(MainGrid.ActualHeight, animationTime);
Xtozero.EasingFunction = new CircleEase();
YtoZero.EasingFunction = new CircleEase();
WidthToFull.EasingFunction = new CircleEase();
HeightTofull.EasingFunction = new CircleEase();
pv.BeginAnimation(Canvas.TopProperty, YtoZero);
pv.BeginAnimation(Canvas.RightProperty, Xtozero);
pv.BeginAnimation(WidthProperty, WidthToFull);
pv.BeginAnimation(HeightProperty, HeightTofull);
SelectedPatientViewer = pv;
e.Handled = true;
And When closing the PatientViewer I revert the animation like this:
DoubleAnimation Xtozero = new DoubleAnimation(initialX, animationTime);
DoubleAnimation YtoZero = new DoubleAnimation(initialY, animationTime);
DoubleAnimation WidthToFull = new DoubleAnimation(PVWidth, animationTime);
DoubleAnimation HeightTofull = new DoubleAnimation(PVHeight, animationTime);
HeightTofull.Completed += (o, a) =>
{
Canvas.SetZIndex(SelectedPatientViewer, 1);
SelectedPatientViewer = null;
};
SelectedPatientViewer.BeginAnimation(Canvas.TopProperty, YtoZero);
SelectedPatientViewer.BeginAnimation(Canvas.RightProperty, Xtozero);
SelectedPatientViewer.BeginAnimation(WidthProperty, WidthToFull);
SelectedPatientViewer.BeginAnimation(HeightProperty, HeightTofull);