Mike Ward
Mike Ward

Reputation: 3321

WPF Animate Insertion into an ItemsControl

Earlier I asked about a fade-in,scroll down animation for items control insertion ( Animate Insertions to ItemsControl ). I got the fade-in working but I'm still perplexed about the items control insertion animation. Below is somethings that "sort of" works.

<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>

I say "sort of" because the list is offset by a negative offset and then animated into position. The "animation to position" looks great, but the "jump" to a negative offset spoils the affect.

In CSS, I would simply animate the height of the inserted item but I haven't puzzled out how to do this in WPF.

Thoughts?

Upvotes: 1

Views: 1865

Answers (1)

Arman Hayots
Arman Hayots

Reputation: 2508

So, I don't know all details, but I think usual way (I'm noob, lol) are:

  1. Add FluidMove behavior to your visual collection
  2. Insert new object with lower width and height, e.g. 1x1
  3. Animate it to actual size via storyboard or manually DoubleAnimation

Upvotes: 2

Related Questions