Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger storyboard on Button click?

Tags:

When I try to start my app with this code it throws an exception saying error text cannot be found.

<Button>
<Button.Triggers>
            <EventTrigger RoutedEvent="Button.Click">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Duration="0:0:05"
                            To="1" From="0"
                            Storyboard.TargetName="ContentGrid"
                            Storyboard.TargetProperty="Opacity" />
                    </Storyboard>
                </BeginStoryboard>
                </EventTrigger>

        </Button.Triggers>
 </Button>

Seems like something wrong with the routed event property. What will fix this exception.

like image 879
shady Avatar asked Apr 26 '16 18:04

shady


1 Answers

I think the best way is to use a Behavior. It's their purpose to extend the functions of basic controls combined with reusability. You can see a snippet right over here on Windows blog (and as I see, it's a part of the framework in Namespace: Microsoft.Xaml.Interactions.Core Assembly: Microsoft.Xaml.Interactions in Microsoft.Xaml.Interactions.dll).

<Button x:Name="button">
   <Interactivity:Interaction.Behaviors>
      <Core:EventTriggerBehavior EventName="Click">
         <Media:ControlStoryboardAction Storyboard="storyboard1"/>
      </Core:EventTriggerBehavior>
   </Interactivity:Interaction.Behaviors>
</Button>
like image 71
mikes Avatar answered Oct 11 '22 11:10

mikes