I often have to animate several things that do not belong to the view in wpf such as the volume of my computer or the position of the mouse. I will like to do so by using wpf storyboards and built in easing functions.
For example let’s say I want to use the following storyboard to animate (decrease volume) on my computer:
<Storyboard x:Key="Storyboard1">
<DoubleAnimationUsingKeyFrames
Storyboard.TargetProperty="someProperty"
Storyboard.TargetName="SomeTarget">
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="0">
<EasingDoubleKeyFrame.EasingFunction>
<CircleEase EasingMode="EaseOut"/>
</EasingDoubleKeyFrame.EasingFunction>
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
and in my code behind I set the volume with the function:
MyVolumeController.SetVolume(0);
as a result I will like to create a function that will look like: (note the function is some sort of pseudo code)
public void AnimateProperty(Storyboard sb, Action<double> onPropertyChange)
{
var property = //sb.targetProperty;
property.OnValueChanged += (a,b)=>{
onPropertyChange(b.newValue);
}
sb.begin();// start animating
}
then I would be able to animate the volume by calling that method as:
AnimateProperty(
(Storyboard)this.FindResource("Storyboard1"), // storyboard
(newVal)=>{MyVolumeController.SetVolume(newVal) // action
);
If you want to animate a property and get notified about value changes while the animation is running, you will have to make that property a dependency property, and attach a PropertyChangedCallback by dependency property metadata.
A typical declaration of such a dependency property would look like the code below (with double as property type here):
Now you can easily animate this property, either by a Storyboard, or much simpler by applying an Animation directly: