C# WPF Stop Storyboard

C# WPF Stop Storyboard



im on programming a slot machine. For spinning the wheel i have follwing animation that looks like:


<Border Height="300" Margin="68,434,1506,335">
<Border.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard >
<Storyboard Name="stb">
<RectAnimation Storyboard.TargetProperty="Background.(ImageBrush.Viewport)"
To="0,0,1,1" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>

</Border.Triggers>
<Border.Background>
<VisualBrush TileMode="Tile" Viewport="0,1,1,1">
<VisualBrush.Visual>
<StackPanel>
<Image Source="test.jpg"></Image>
<Image Source="test.jpg"></Image>
<Image Source="test.jpg"></Image>
</StackPanel>
</VisualBrush.Visual>
</VisualBrush>
</Border.Background>
</Border>
<Button Name="cmdStop"/>



How can i start/stop from Code behind? Thanks for your help.



EDIT



I want to it to start / stop with a button not when a page is loaded. how can i solve this ?
The problem is the button should be outside of the border... How can i access the button?




2 Answers
2



Have you tried to pause the anmation?



A good example is found here and when you want to pause it from the Code-Behind, use DataTriggerinstead of EventTrigger.


DataTrigger


EventTrigger



And maybe you are interested in the "Triple the speed of the Storyboard" too.



Edit
Your Question "I want to it to start / stop with a button not when a page is loaded. how can i solve this ?" cant be answered because when the Page (Border?) is not loaded, the animation cant be running. I Suggest to hide the Page/Border and only make it visible when the animation is paused.



I fiddled a bit around and tried to solve it with the Visibility


<!-- This Border is animated. -->
<Border Height="300" Margin="68,434,1506,335" >
<Border.Style>
<Style TargetType="x:Type Border">
<!-- Here is your animation -->
<Style.Triggers>
<EventTrigger RoutedEvent="Border.Loaded">
<BeginStoryboard Name="RandomStoryboard">
<Storyboard >
<RectAnimation Storyboard.TargetProperty="Background.(ImageBrush.Viewport)"
To="0,0,1,1" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
<!-- Stop the animation at the Start -->
<PauseStoryboard BeginStoryboardName="RandomStoryboard" />
</EventTrigger>
<!-- Control the animation according to the Togglebutton State -->
<DataTrigger Binding="Binding Path=IsChecked, ElementName=SpinControl" Value="True">
<DataTrigger.EnterActions>
<ResumeStoryboard BeginStoryboardName="RandomStoryboard" />
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<PauseStoryboard BeginStoryboardName="RandomStoryboard" />
</DataTrigger.ExitActions>
<!-- Hide the Border while the animation is running -->
<Setter Property="Border.Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>

<!-- This Button Controls the Animated Border -->
<ToggleButton Name="SpinControl">
<ToggleButton.Style>
<Style TargetType="x:Type ToggleButton">
<Setter Property="Content" Value="Start"/>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value="Stop"/>
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>



Note: The Style Section of the ToggleButtonis optional, it changes only the Content from Start to Stop (and vice versa).


Style


ToggleButton


Content



Note2: Dont forget to insert your VisualBrush in the Border, otherwise the animation is not recognized.


VisualBrush





can u show me how to use a datatrigger?
– Maddin
Aug 31 at 7:55





I just have seen your Edit. In this Example they already use Buttons, therefore a DataTriggeris not necessary.
– LittleBit
Aug 31 at 7:59


DataTrigger





Updated my answer to fit your Edit
– LittleBit
Aug 31 at 10:02





Thank Works great ! :) One additional question: if i stop the animation is the last picture from botom cutted and begins from top. How did i call them to complete the actual spin?
– Maddin
Aug 31 at 12:32





Unfortunately, i have never needed such an animation behaviour. But i suggest you to take a look at Storyboard.Seek which moves the animation for- or backward a specified amount. + I' m always glad to help a thankful person.
– LittleBit
Aug 31 at 13:14


Storyboard.Seek



In general you can start/stop a storyboard using the start and stop method of the storyboard object.



In your example should be stb.Start() or stb.Stop()





well i alrdy tried this but the animation still does not stop
– Maddin
Aug 31 at 7:17



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế