1

之前我询问了项目控件插入的淡入、向下滚动动画(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 中做到这一点。

想法?

4

1 回答 1

2

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

  1. 将 FluidMove 行为添加到您的视觉集合
  2. 插入具有较低宽度和高度的新对象,例如 1x1
  3. 通过情节提要或手动 DoubleAnimation 将其设置为实际大小
于 2012-10-13T14:09:40.740 回答