views:

38

answers:

0

Hi all. I'm using a third-party GridLengthAnimation class which is built to animate just like a DoubleAnimation class. I've used it many times before and it has never failed me - until now.

I am trying to use it to expand/collapse part of my form based on the user checking a +/- checkbox to expand/collapse it. You know the drill. The animation works fine using absolute gridlengths, but not star-sizing. I've used it with star-sizing before.

My C# code:

private void checkbox_expand_process_Click(object sender, RoutedEventArgs e)
{
    RowDefinition target = row_process_info;
    //MessageBox.Show(target.Height.Value.ToString());

    //Construct a storyboard and animate it
    Storyboard ProcExpandCollapse = new Storyboard();
    Duration HalfSecond = new Duration(TimeSpan.FromSeconds(0.5));
    ProcExpandCollapse.Duration = HalfSecond;

    GridLength Star = new GridLength(1, GridUnitType.Star);
    GridLength Zero = new GridLength(0, GridUnitType.Star);

    GridLengthAnimation Anim = new GridLengthAnimation();
    Anim.Duration = HalfSecond;
    ProcExpandCollapse.Children.Add(Anim);
    Storyboard.SetTarget(Anim, target);
    Storyboard.SetTargetProperty(Anim, new PropertyPath("Height"));

    Anim.From = target.Height;

    if (((CheckBox)sender).IsChecked.Value)
    {
        Anim.To = Star;
    }
    else
    {
        Anim.To = Zero;
    }

    ProcExpandCollapse.Begin();
}

My XAML that it interacts with:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" Name="row_process_info"/>
    </Grid.RowDefinitions>

    <WrapPanel Name="wrap_process_expand" Grid.Row="0">
        <CheckBox Name="checkbox_expand_process" 
                  Template="{StaticResource ExpandCollapse}" 
                  Click="checkbox_expand_process_Click"/>
        <!--For debugging-->
        <Label Content="{Binding ElementName=row_process_info, Path=Height}" />
    </WrapPanel>

    <!--Grid containing process legend and checkboxes-->
    <Grid Grid.Row="1" Name="grid_process_info">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid Margin="0,3,0,10" Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <!-- Content -->
        </Grid>
        <WrapPanel Name="wrap_process_info" Grid.Row="1" >
            <!-- Content -->
        </WrapPanel>
    </Grid>
</Grid>

What happens: literally nothing. It does not change height even a single pixel when the animation is run. I put a label there whose content is bound to the row height just to see how it's changing over time.

Other than possible a flaw with the animation class, can you see any reason this would not work?