Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize WPF UserControl without all children "jumping around"

Is there a way to resize a WPF UserControl in such a way that the child controls don't flop around?

I've got this Storyboard:

<Storyboard>
    <DoubleAnimation Storyboard.TargetProperty="Height" Storyboard.TargetName="MyUserControl" To="145" From="0" Duration="0:0:1" />
</Storyboard>

This works great; it takes my control from Height Zero to Height 145 -- the problem is that as the Height property changes, all of the child controls inside start jumping around, I suspect, due to their HorizontalAlignment and VerticalAlighment properties. Is there a way I can disable that until the animation is finished?

I'm trying to create the illusion of this UserControl "sliding" into view -- so I'm open to other approaches if I'm going about this wrong.

like image 511
Nate Avatar asked Feb 23 '11 20:02

Nate


2 Answers

Everything is "jumping around" because every time the Height of the control is changed all the containing controls reposition themselves according to the new available space.

To achieve the desired effect you should use RenderTransform instead of changing the actual Height of the control.

Here is how you can do that. First, add ScaleTransform as a value of RenderTransform property on your control:

<MyUserControl.RenderTransform>
    <ScaleTransform ScaleY="0" />
</MyUserControl.RenderTransform>

Then, modify target property of your animation to change the ScaleY property of the transform:

<Storyboard>
    <DoubleAnimation Storyboard.TargetProperty="RenderTransform.(ScaleTransform.ScaleY)" Storyboard.TargetName="MyUserControl" To="145" Duration="0:0:1" />
</Storyboard>

Update:

Another way to achieve the slide-into-view effect is to use TranslateTransform with negative Y value:

<MyUserControl.RenderTransform>
   <TranslateTransform Y="-1000" />
</MyUserControl.RenderTransform>

And then, animate its Y property to 0:

<Storyboard>
    <DoubleAnimation Storyboard.TargetProperty="RenderTransform.(TranslateTransform.Y)" Storyboard.TargetName="MyUserControl" To="0" Duration="0:0:1" />
</Storyboard>
like image 130
Pavlo Glazkov Avatar answered Oct 13 '22 19:10

Pavlo Glazkov


When you animate the Height or Width the UserControl tells its children also to readjust, that is the way the layout system works in WPF. The 'Jumping around' will really depends upon the kind of Layout (Grid,StackPanel etc..) you using.

But looks like, for your scenario it will be better to use RenderTransform ScaleTransform ScaleY animation instead of animating the Height. Rendertransform wont disturb your layout arrangements of the UserControl.

like image 27
Jobi Joy Avatar answered Oct 13 '22 18:10

Jobi Joy