views:

29

answers:

2

I created a ColorAnimation for a SpotLight-object, but it doesn't seem to work. What am I doing wrong?

ColorAnimation mouseEnterColorAnimation = new ColorAnimation();
mouseEnterColorAnimation.To = Colors.Red;
mouseEnterColorAnimation.Duration = TimeSpan.FromSeconds(5);
Storyboard.SetTargetName(mouseEnterColorAnimation, "MyAnimatedBrush");

Storyboard.SetTargetProperty(mouseEnterColorAnimation, new PropertyPath(SpotLightAuditorium.Color));
Storyboard storyboard = new Storyboard();
storyboard.RepeatBehavior = RepeatBehavior.Forever;
storyboard.Children.Add(mouseEnterColorAnimation);
storyboard.Begin(this);
+1  A: 

When using Storyboard.SetTargetName, the name must be the value of the actual Name property of the FrameworkElement instance where you want to animate a property. So in your case probably an instance of the SpotLightAuditorium control:

Storyboard.SetTargetName(mouseEnterColorAnimation, mySpotlightAuditorium.Name);

The propertypath must be a reference to an actual dependency property:

Storyboard.SetTargetProperty(mouseEnterColorAnimation, new PropertyPath(SpotLightAuditorium.ColorProperty));

If you want to animate a Brush directly (wich doesn't have a Name property) you have to register the name of the brush in the current Page/UserControl/Window using RegisterName This the same as using XAML x:Name.

ALternativlely you can use the following approach for elements that derive from Animatable:

ColorAnimation mouseEnterColorAnimation = new ColorAnimation();
mouseEnterColorAnimation.To = Colors.Red;
mouseEnterColorAnimation.Duration = TimeSpan.FromSeconds(5);
myAnimatedBrush.BeginAnimation(SolidColorBrush.ColorProperty, null); // remove the old animation to prevent memoryleak
myAnimatedBrush.BeginAnimation(SolidColorBrush.ColorProperty, mouseEnterColorAnimation);
bitbonk
SpotlightAuditorium is a concrete instance of Spotlight which doesn't seem to have a Name-attribute.Also I don't get what you mean with SpotLightAuditorium.ColorProperty.
Hedge
I was assuming that your SpotLightAuditorium would get a Color dependency property, wich you would then use for animation. If Spotlight doesn't have a name property you will have to use the RegisterName method as described in my answer to set the name.
bitbonk
How do I set a Color dependency property?I also don't understand why I need a brush instead of a regular color (as SpotLight (3D-Light)) only has a color-attribute.Sorry for asking that much.
Hedge
Ah - we are talking *System.Windows.Media.Media3D.SpotLight* here. This class has a Color dependency property. You would use it for animation (no Brush invloved- `"MyAnimatedBrush"` made me aussume that you *might* have a brush somewhere. So you would call RegsiterName for the Spotlight, and then `Storyboard.SetTargetName(mouseEnterColorAnimation, mySpotlightAuditorium.Name)` and then `Storyboard.SetTargetProperty(mouseEnterColorAnimation, new PropertyPath(SpotLight.ColorProperty));`
bitbonk
YAY! It finally works.Why doesn't SetTarget instead of SetTargetName work?
Hedge
A: 

You didn't register the brush's name with the page so that it can be targeted by storyboards:

SolidColorBrush myAnimatedBrush = new SolidColorBrush();
myAnimatedBrush.Color = ?? choose a color

this.RegisterName("MyAnimatedBrush", myAnimatedBrush);
vulkanino