开发者

Resize WPF UserControl without all children "jumping around"

开发者 https://www.devze.com 2023-02-13 05:02 出处:网络
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:

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 jump开发者_高级运维ing 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.


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>


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.


To add to Pavlov's updated answer, I had to use a blank CompositeTransform instead. Maybe it is due to using the latest Blend for Visual Studio 2012 update 1, but this worked (targeting Windows Phone 8):

    <MyUserControl.RenderTransform>
        <CompositeTransform />
    </MyUserControl.RenderTransform>
0

精彩评论

暂无评论...
验证码 换一张
取 消