0

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);
javirs
  • 1,049
  • 26
  • 52
  • That sounds horrible. Using a Canvas for element layout is a bad idea. Use a more appropriate Panel, like Grid or DockPanel. That said, animations hold their end value unless you set their FillBehavior to Stop. – Clemens Aug 28 '19 at 07:47
  • Dude! The fill Behaviour thing did the trick. My problem with Panels ListBox and so on is that I cannot animate from there to full screen. I need to handle the ZIndex to move the item to the top and this can only be done with Canvas. Isn't it ? – javirs Aug 28 '19 at 07:52
  • No, ZIndex work in all Panels. – Clemens Aug 28 '19 at 07:53
  • imagine a layout with a colum in the left for navigation and main area in the right. I'd use a grid with two colums. A Border with Stack panel in the navigation. And a Wrappanel with the items in the right. How can I make my items in the wrappanel grow to full screen when clicked? – javirs Aug 28 '19 at 07:56
  • Try a DockPanel. – Clemens Aug 28 '19 at 07:58
  • I've used dock panel. And I don't know how to maximize items outside their immediate parent – javirs Aug 28 '19 at 08:01
  • Maybe ask a question on StackOverflow. – Clemens Aug 28 '19 at 08:06
  • There I go :) Thanks – javirs Aug 28 '19 at 08:21
  • @Clemens would you post the fillbehaviour thing as an answer? – javirs Aug 28 '19 at 08:21
  • @Clemens Here's the other question in case you can help there :) https://stackoverflow.com/questions/57688132/wpf-layout-design-that-allows-maximizing-an-item – javirs Aug 28 '19 at 10:04

0 Answers0