我正在尝试在 StackPanel 的可见性从宽度 0 变为自动宽度时对其进行动画处理,这是我目前所拥有的:
<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>
有人能解释一下我如何实现这个动画吗?以我尝试的方式可能是不可能的吗?
谢谢,
亚历克斯。
这是我整理的一个快速模型项目。
在窗口的加载事件中,我只需将堆栈面板的可见性设置为可见,然后它就会从左到右扩展以适应其容器宽度...希望这能满足您的需求。
需要注意的一些事项:
- 您必须预先定义缩放变换,否则动画将无法播放。
- 如果您在动画中省略 To,它将动画回到默认值。
这是代码:
<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)