Question

I have a ListView, that is defined very simply in my XAML like so

<ListView Name="myListVew" MaxHeight="200" Visibility="Collapsed">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Line" Width="Auto" DisplayMemberBinding="{Binding Line}" />
            <GridViewColumn Header="Error" Width="Auto" DisplayMemberBinding="{Binding Error}" />
        </GridView>
    </ListView.View>
</ListView>

When I want the ListView to appear, I want to animate the height of the list view from 0. The problem is, I don't know the final height of the ListView since it will depend on how many items are shown inside it. Is there anyway of doing this?

Was it helpful?

Solution

Don't you hate it when you search for hours for a solution, post a question to a forum and then find the answer yourself 10 minutes later?

Anyway, I got it working by applying a layout scale transform like so

<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ErrorDisplay" Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
    <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
    <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1"/>
</DoubleAnimationUsingKeyFrames>

OTHER TIPS

This isn't exactly the same scenario but it may help someone else.

I had a border that had a height of Auto and I wanted it to animate to a specific height.

My border looked like this:

<Border Name="ContainerBorder">
  <!-- Stuff... -->
</Border>

I created this storyboard:

<Storyboard x:Key="EditIn">
  <DoubleAnimation Storyboard.TargetProperty="Height"
                   Storyboard.TargetName="ContainerBorder"
                   Name="BorderAnimation"
                   To="45"
                   Duration="0:0:0.8" />
</Storyboard>

The button that triggered the animation had some codebehind:

private void Edit_Click(object sender, RoutedEventArgs e) {
  Storyboard sb = (Storyboard)FindResource("EditIn");
  //Find the border animation
  DoubleAnimation da = (DoubleAnimation)sb.Children.Where(t => t.Name == "BorderAnimation").FirstOrDefault();
  if (da != null) { da.From = ContainerBorder.ActualHeight; }
  sb.Begin(this);
}

So, I simply found the border and set the "From" property of the animation to the FrameworkElement's ActualHeight.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top