WPF动画StackPanel的宽度从0到自动? [英] WPF animating a StackPanel's width from 0 to Auto?
本文介绍了WPF动画StackPanel的宽度从0到自动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为StackPanel设置动画,使其可见性从0的宽度增加到其自动宽度,这是我目前的状态:
I am trying to animate a StackPanel when its visibility changed to grow from a width of 0 to its automatic width, here is what I have at the moment:
<Trigger Property="Visibility" Value="Visible">
<Setter Property="Width" Value="0"></Setter>
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Width" Duration="0:0:1">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<System:Double>NaN</System:Double>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
有人可以解释我如何实现此动画吗?
Can someone explain how I might achieve this animation? Is it maybe not possible in the way I am trying to do it?
谢谢,亚历克斯.
推荐答案
这是我一起提交的快速样机项目.
Here is a quick mockup project I threw together.
在Window的Loaded事件中,我只是将堆栈面板的可见性设置为Visible,并且它会扩展以适应其容器的宽度,从左到右...希望能满足您的需求.
In the Window's Loaded event, I simply set the stackpanel's visibility to Visible and it expands to fit its container width from left to right... Hopefully that's suits your needs.
一些注意事项:
- 您必须预先定义比例变换,否则动画将无法播放.
- 如果在动画中省略到",它将动画化回默认值.
这是代码:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="600" Loaded="Window_Loaded">
<Border HorizontalAlignment="Center" Width="300" Background="Gainsboro">
<Border.Resources>
<Style TargetType="StackPanel" x:Key="expand">
<Setter Property="RenderTransform">
<Setter.Value>
<ScaleTransform ScaleX="1"/>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Visibility" Value="Visible">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX"
From="0"
Duration="0:00:01"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
</Style.Triggers>
</Style>
</Border.Resources>
<StackPanel x:Name="stackpanel" Background="Gray" Visibility="Collapsed" Style="{StaticResource expand}"/>
</Border>
</Window>
这篇关于WPF动画StackPanel的宽度从0到自动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文