Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi all,

I have a "Storyboard" in window resources in XAML of WPF and I want change it (value="375") dynamically in code C# and at run-time.
I also used "Binding, StaticResource and DynamicResource", but it is an error.
You can test it yourself and please help me so about this.

HTML
<window.resources>
    <storyboard x:key="myStoryboard" xmlns:x="#unknown">
        <doubleanimationusingkeyframes storyboard.targetproperty="(FrameworkElement.Width)" storyboard.targetname="viewbox">
            <easingdoublekeyframe keytime="0:0:1.5">Value="375">
                <easingdoublekeyframe.easingfunction>
                    <exponentialease easingmode="EaseIn" exponent="10" />
                </easingdoublekeyframe.easingfunction>
            </easingdoublekeyframe>
        </doubleanimationusingkeyframes>
    </storyboard>
</window.resources>


Thanks in advance.
Posted
Updated 28-Oct-11 12:49pm
v2

Here's one way to get there using code:

C#
EasingDoubleKeyFrame keyFrame = ((this.Resources["myStoryboard"]
    as Storyboard).Children[0]
    as DoubleAnimationUsingKeyFrames).KeyFrames[0]
    as EasingDoubleKeyFrame;

keyFrame.Value = 100.5;


That said, you should be binding it to a property in the view-model and then changing that.
 
Share this answer
 
Comments
hzawary 28-Oct-11 19:50pm    
My 5, Very good!

I don't understand,may more explain "That said, you should be binding it to a property in the view-model and then changing that." Is this other way?
Nish Nishant 28-Oct-11 20:10pm    
What I meant is that it's better to rely on data binding here. Because here I've made several assumptions about the Xaml. If you ever change the Xaml in future that code will break. But if you bind to a proeprty then it will continue to work.
hzawary 28-Oct-11 20:45pm    
Ok! May tell me that How is it done in this case?
setafonnix 16-Jan-14 11:05am    
This is Solve my problem !, it's can help me to make easing with dynamic values
in winRT, i'm just change...
this.Resources["myStoryboard"] TO mySoryboard.Storyboard.Children[0]
by the way, thanks a lot.
Adjust KeyFrame.Value and restart the Animation or Storyboard.

C#
Storyboard storyboard = Storyboard;
FrameworkElement frameworkElement = Ellipse;

// Restart
storyboard.Stop(frameworkElement);
storyboard.Begin(frameworkElement);


That worked fine for me!
 
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