I set out to create a simple button toggle animation. Click a button once the button's background color changes from purple to red. Click it again and it changes from red to purple. It's turned out to be really difficult.
First, animations don't seem to be modifiable. When I try to swap the From and to Colors in code on a ColorAnimation WPF throws an exception complaining that the animation is 'frozen'. I can't find a way to unfreeze the animation. Another problem is that since the animation always seems to be frozen I can never even assign an event handler to its 'Completed' event to get a notification of when an animation is complete. (If I assign the event handler in xaml instead of in code, it is just ignored. It still never fires). I just wanted to do something simple like 'whenver the animation finishes, swap the from and to colors.' Now I'm completely at a loss for how to accomplish something like this.
So my general question is 'how do I modify animations in code'. My specific question is 'how do I create a toggle button that animates between two or more states when it is clicked'.
-Ryan
rfuller987@hotmail.com

Creating a button toggle animation (why are animations unable to be modified in WPF?)
Cassie1
Greetings,
I'm also having problems with animations. It seems nealy every one of Microsoft's examples are how to animate a button (why you'd want to do this I can't guess). I want a 3D cube animated by changing the view position.
private void button_animate_click(object sender, EventArgs e){
PerspectiveCamera pCam = (PerspectiveCamera)vpt.Camera; //Point3D pPos = pCam.Position; //if (pPos.Z > -5) //{ // pPos.Z = pPos.Z - 2.5; //} //else //{ // pPos.X = pPos.X - 2.5; //} //pCam.Position = pPos; //Vector3D pLook = pCam.LookDirection; //if (pLook.Z < 5) //{ // pLook.Z = pLook.Z + 2.5; //} //else //{ // pLook.X = pLook.X + 2.5; //} //pCam.LookDirection = pLook; Point3DAnimation p = new Point3DAnimation();p.From =
new Point3D(5, 5, 5);p.To =
new Point3D(5, 5, -5);p.Duration =
new Duration(TimeSpan.Parse("0:0:5"));vpt.Camera.BeginAnimation(pCam.PositionProperty, p);
}
The example above works if you use the commented out portion and delete the last 5 lines. This uses a button to just add 2 to the position.
Trying an animation doesn't work, there is always an error in the last line. Now if I change the last line to
button1. BeginAnimation(Button.WidthProperty, p); // (They do like buttons :-)
a button is animated when used with a DoubleAnmation instead of a Point3DAnimation.
Who can show me how to write the BeginAnimation statement for a camera positionThanks, Theron Wierenga
mahmut
I don't know about the frozen problem but here is an example of how you can do your animation in xaml :
<
Grid><Grid.Resources>
<Style x:Key="{x:Type ToggleButton}" TargetType="{x:Type ToggleButton}">
<Setter Property="Background" Value="Purple"/>
<Style.Triggers>
<Trigger Property="IsChecked" Value="true">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="(ToggleButton.Background).(SolidColorBrush.Color)" To="Red" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="(ToggleButton.Background).(SolidColorBrush.Color)" To="Purple" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<ToggleButton>
MyToggleButton
</ToggleButton>
</Grid>
Guillaume
WillHart
My knowledge here is limited, but I think you can clone a Freezable, so something like:
Storyboard clonedStoryboard = oldStoryboard.Clone();
clonedStoryboard.Begin(this);
Regarding the other question, I found this video on the internet to be very useful - it shows you have you can achieve something similar using Expression Interactive Designer.
http://labs.nukeation.net/videos/wpf/exploring/01/default.html
Thanks,
-Unni