WPF Animate插入到ItemsControl中

时间:2012-10-13 13:49:39

标签: wpf xaml

之前我曾询问有关项目控件插入的淡入,向下滚动动画(Animate Insertions to ItemsControl)。我得到了淡入效果,但我仍然对项目控件插入动画感到困惑。以下是“有点”的事情。

<Grid>
    <ScrollViewer>
        <ItemsControl Name="TimelineItems"
                      ItemsSource="{Binding Timeline}"
                      Style="{StaticResource TimelineStyle}"
                      ItemContainerStyle="{StaticResource TweetItemStyle}">
            <ItemsControl.RenderTransform>
                <TransformGroup>
                    <TranslateTransform />
                </TransformGroup>
            </ItemsControl.RenderTransform>
            <ItemsControl.Triggers>
                <EventTrigger RoutedEvent="FrameworkElement.SizeChanged">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="TimelineItems"
                                                           Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.Y)">
                                <EasingDoubleKeyFrame KeyTime="0"
                                                      Value="-50" />
                                <EasingDoubleKeyFrame KeyTime="0:0:0.5"
                                                      Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </ItemsControl.Triggers>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid Name="MyGrid"
                          Background="{Binding TweetType, Converter={StaticResource tweetTypeConverter}}"
                          VerticalAlignment="Top"
                          HorizontalAlignment="Left">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Style="{StaticResource TweetImageColumnStyle}" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <Rectangle Grid.Column="0"
                                   Style="{StaticResource TweetImageStyle}">
                            <Rectangle.Fill>
                                <ImageBrush ImageSource="{Binding ProfileImageUrl}" />
                            </Rectangle.Fill>
                        </Rectangle>
                        <StackPanel Grid.Column="1">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0"
                                           Style="{StaticResource TweetNameStyle}"
                                           Text="{Binding Name}" />
                                <TextBlock Grid.Column="1"
                                           Style="{StaticResource TweetTimeStyle}"
                                           Text="{Binding TimeAgo}" />
                            </Grid>
                            <Controls:TextBlockMarkup Grid.Row="1"
                                                      Grid.Column="1"
                                                      Markup="{Binding MarkupText}"
                                                      Style="{StaticResource TweetStyle}" />
                        </StackPanel>
                        <Separator Grid.Row="2"
                                   Grid.ColumnSpan="2"
                                   Style="{StaticResource TweetSeparatorTop}" />
                        <Separator Grid.Row="3"
                                   Grid.ColumnSpan="2"
                                   Style="{StaticResource TweetSeparatorBottom}" />
                    </Grid>

                    <DataTemplate.Resources>
                        <Storyboard x:Key="ItemAnimation"
                                    AutoReverse="False">
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="MyGrid"
                                                           Storyboard.TargetProperty="(UIElement.Opacity)">
                                <EasingDoubleKeyFrame KeyTime="0"
                                                      Value="0" />
                                <EasingDoubleKeyFrame KeyTime="0:0:0.8"
                                                      Value="1" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                    </DataTemplate.Resources>

                    <DataTemplate.Triggers>
                        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                            <BeginStoryboard Storyboard="{StaticResource ItemAnimation}" />
                        </EventTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>
</Grid>

我说“有点”因为列表被负偏移量偏移然后动画到位置。 “动画到位置”看起来很棒,但“跳跃”到负偏移会破坏效果。

在CSS中,我只是简单地设置插入项目的高度动画,但我并不知道如何在WPF中执行此操作。

思想?

1 个答案:

答案 0 :(得分:2)

所以,我不知道所有的细节,但我认为通常的方式(我是noob,哈哈)是:

  1. 将FluidMove行为添加到您的可视化集合
  2. 插入宽度和高度较低的新对象,例如的1x1
  3. 通过故事板或手动DoubleAnimation
  4. 将其设置为实际大小