动画不能与WPF VisualStateManager一起播放

时间:2012-04-17 11:54:16

标签: wpf animation triggers visualstatemanager

我希望添加到ItemsControl中的项目在添加时为高度设置动画。以下示例使用触发器完成了这项工作,但我无法使用非固定高度的项目(在这种情况下为50)。

<ItemsControl ItemsSource="{Binding Notifications}">
<ItemsControl.Resources>
    <DataTemplate DataType="{x:Type Notifications1:Notification}">
        <Button x:Name="ItemButton"
                ClipToBounds="True"
                Height="0">
            <Button.Template>
                <ControlTemplate>
                    <Notifications:NotificationTile />
                </ControlTemplate>
            </Button.Template>
        </Button>
        <DataTemplate.Triggers>
            <EventTrigger RoutedEvent="FrameworkElement.Loaded"
                            SourceName="ItemButton">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Height"
                                            Storyboard.TargetName="ItemButton"
                                            Duration="0:0:0.5"
                                            To="50" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</ItemsControl.Resources>

然后我尝试使用VisualStateManager来执行动画,因此切片会增长到他们需要的任何高度。在下面的示例中,项目以正确的大小添加,但不执行动画。我假设EventTrigger甚至没有被解雇?

任何想法都非常感激!

<ItemsControl ItemsSource="{Binding Notifications}"
            Width="230"
            ScrollViewer.HorizontalScrollBarVisibility="Hidden"
            HorizontalContentAlignment="Stretch">

<ItemsControl.Resources>
    <DataTemplate DataType="{x:Type Notifications1:Notification}">
        <Button x:Name="ItemButton"
                ClipToBounds="True"
                Command="{Binding DataContext.ItemClicked, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
                CommandParameter="{Binding}"
                Visibility="{Binding IsVisible, Converter={StaticResource boolToVisibilityConverter}}">
            <Button.Template>
                <ControlTemplate>
                    <Notifications:NotificationTile />
                </ControlTemplate>
            </Button.Template>

            <VisualStateManager.CustomVisualStateManager>
                <is:ExtendedVisualStateManager />
            </VisualStateManager.CustomVisualStateManager>
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup is:ExtendedVisualStateManager.UseFluidLayout="True">
                    <VisualStateGroup.Transitions>
                        <VisualTransition GeneratedDuration="0:0:2" />
                    </VisualStateGroup.Transitions>
                    <VisualState x:Name="Collapsed">
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="(FrameworkElement.Height)"
                                                Storyboard.TargetName="ItemButton"
                                                Duration="0"
                                                To="0" />
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="Expanded">
                        <Storyboard>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)"
                                                            Storyboard.TargetName="ItemButton">
                                <DiscreteDoubleKeyFrame KeyTime="0"
                                                        Value="NaN" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>

            <i:Interaction.Triggers>
                <i:EventTrigger SourceName="ItemButton"
                                EventName="(FrameworkElement.Loaded)">
                    <is:GoToStateAction StateName="Expanded" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>
    </DataTemplate>
</ItemsControl.Resources>

1 个答案:

答案 0 :(得分:0)

也许这有点偏离主题,对不起。我不确定我完全理解你的问题。 无论如何,通常不建议只有“To”的动画,因为“From”最初设置为Double.NaN,而DoubleAnimation在这种情况下不起作用。