Search code examples
c#wpfanimationstoryboard

Why does the storyboard.target not get animated?


I want to animate a stack panel on button click, the Button I click does animate while the StackPanel doesn't

this is in the button ControlTemplate

<EventTrigger RoutedEvent="Click">
    <BeginStoryboard>
        <Storyboard>
            <DoubleAnimation Storyboard.Target="{Binding ElementName=menus}"        
                Storyboard.TargetProperty="Width"
                From="0" To="1500"
                Duration="0:0:2" />
        </Storyboard>
    </BeginStoryboard>
</EventTrigger>

This is in the xaml

<StackPanel x:Name="menus" Orientation="Horizontal"
    VerticalAlignment="Bottom"
    HorizontalAlignment="Center"
    Margin="160 0 0 120">
        <Button Margin="30" Content="Open Account" Style="{StaticResource MenuButton}" />
        <Button Margin="30" Content="Instant Card Printing" Style="{StaticResource MenuButton}" />
        <Button Margin="30" Content="Banking Services" Style="{StaticResource MenuButton}" />
        <Button Margin="30" Content="Bill Payment" Style="{StaticResource MenuButton}" />
        <Button Margin="30" Content="Support" Style="{StaticResource MenuButton}" />
</StackPanel>

I want the stack panel to animate from left to right once the button is clicked


Solution

  • I'm not sure what about your code is causing your example to fail. There is probably something going on in the XAML that you didn't post. Below is a complete and self-contained example that functions exactly as you described:

    <StackPanel>
        <Button Content="Clicky Button" Margin="10" HorizontalAlignment="Left">
            <Button.Triggers>
            <EventTrigger RoutedEvent="PreviewMouseDown">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.Target="{Binding ElementName=menus}"        
                          Storyboard.TargetProperty="Width"
                          From="0" To="500"
                          Duration="0:0:2" />
                    </Storyboard>
                </BeginStoryboard>
                </EventTrigger>
            </Button.Triggers>
        </Button>
        <StackPanel x:Name="menus" Orientation="Horizontal" Width="0"
                    VerticalAlignment="Bottom" HorizontalAlignment="Center">
            <Button Content="Open Account" />
            <Button Content="Instant Card Printing" />
            <Button Content="Banking Services" />
            <Button Content="Bill Payment" />
            <Button Content="Support"/>
        </StackPanel>
    </StackPanel>
    

    When you click on 'Clicky Button' the StackPanel grows to a width of 500 over the course of 2 seconds.