Click here to Skip to main content
15,942,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm trying to get an animation to play when a bool changes using triggers.
I have my Animation working on a button click as shown below:
XML
<Grid.Triggers>
                <EventTrigger SourceName="StartBtn" RoutedEvent="Button.Click">
                    <EventTrigger.Actions>
                        <BeginStoryboard x:Name="MyStoryboard">
                            <Storyboard x:Name="Pulse_Animation_Story" RepeatBehavior="1x">
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="regularPolygon">
                                    <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                                    <EasingDoubleKeyFrame KeyTime="0:0:4" Value="4320"/>
                                </DoubleAnimationUsingKeyFrames>
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="regularPolygon1">
                                    <EasingDoubleKeyFrame KeyTime="0" Value="90"/>
                                    <EasingDoubleKeyFrame KeyTime="0:0:4" Value="450"/>
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>


However when I attempt to change the button click to a data trigger like so:

XML
<Grid.Style>
               <Style >
                   <Style.Triggers>
                       <DataTrigger Binding="{Binding Path=HasAnimations}" Value="True">
                           <DataTrigger.EnterActions>
                               <!--REFERENCE STORY1 HERE-->
                               <BeginStoryboard x:Name="MyStoryboard2">
                                   <Storyboard x:Name="Pulse_Animation_Story2" RepeatBehavior="1x">
                                       <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="regularPolygon">
                                           <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                                           <EasingDoubleKeyFrame KeyTime="0:0:4" Value="4320"/>
                                       </DoubleAnimationUsingKeyFrames>
                                       <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="regularPolygon1">
                                           <EasingDoubleKeyFrame KeyTime="0" Value="90"/>
                                           <EasingDoubleKeyFrame KeyTime="0:0:4" Value="450"/>
                                       </DoubleAnimationUsingKeyFrames>
                                   </Storyboard>
                               </BeginStoryboard>
                           </DataTrigger.EnterActions>
                       </DataTrigger>

                   </Style.Triggers>
               </Style>
           </Grid.Style>



I get the error "TargetName property cannot be set on a Style Setter"

Is it possible to reference the previously created storyboard to overcome this issue?
Or how do i access my polygons which I want to animate?

Thank you
Posted

So after reading many guides and tutorials trying to link a member variable bool to a data trigger with no luck I created my own solution.


If anyone stumbles upon this in the future or is new to WPF controls or Xaml and you are able to edit the Xaml.cs file and want a programmatic solution....


First of all I pulled my story board out of the trigger. and dumped it in resources like so:


xmlns:NameSpace="clr-namespace:LetMakeACLock"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
mc:Ignorable="d"

d:DesignWidth="100" d:DesignHeight="100" Width="100" Height="100" Background="{x:Null}" BorderBrush="#FF009B76">

<button.resources>
<storyboard x:name="Animate_Hands" x:key="Animate_Hands" repeatbehavior="Forever">
***DO ANIMATION HERE**







Next in the cs code I created my boolean property and added a function on set:

private bool m_playAnimation;
public bool playAnimation
{
get { return m_playAnimation; }
set
{
m_playAnimation= value;
if(m_playAnimation)
{
play();
}
}
}

and in my play function I put:

private void play()
{
Storyboard CurrentAnimation = (Storyboard)this.Resources[0];
CurrentAnimation.Begin();
}

Resource accepts an int index or a string relating to x:Name

Since I'm new to Xaml and WPF this may not be the best solution, but given time restraints it works well enough for me.

I am still looking for an answer on how to set a member bool to a data trigger like the one I wanted. but I suspect merely setting a bool does not raise an event that the trigger would require.
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900