How to sync animations in user controls in WPF

I am creating several custom controls in a wrapper panel. I have a view model associated with usercontrol, and I have an animation called by a property in the view model. Very easy to switch colors from red to transparent to simulate blinking.

<Storyboard x:Key="alertAnimation" RepeatBehavior="Forever" AutoReverse="True" > <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="TileBorder" BeginTime="00:00:00" RepeatBehavior="Forever" AutoReverse="True" > <DiscreteColorKeyFrame Value="Red"/> <DiscreteColorKeyFrame KeyTime="00:00:00.500" Value="Transparent" /> </ColorAnimationUsingKeyFrames> </Storyboard> 

This works great. However, since I load multiple user elements asynchronously, the blinking animations do not sync, so they all blink at different times. Now there is a requirement that all blinking on the screen should all blink at the same speed / time. Is there a way to sync these animations? It seems I cannot find an example that is suitable for what I am trying to execute. Is there a way to use ParallelTimeline, add all animations to it and start / stop them from one controller? Any examples how to do this?

EDIT 4/20 Would it be better to have the animation defined in the style.xaml file and have a β€œglobal” storyboard in which each control adds its own β€œblinking” animation and the main user interface launches the storyboard?

+6
source share
2 answers

I could not find a good way to control multiple animations in multiple user controls, so I did not use animation in usercontrol, but instead used animation to just change the property dependency in a static object and have all user controls that should blink in some mods associated with this property. There are various ways to get the desired results that I wanted, but in the end I now have a way for all my controls to know when it is blinking and then turned off so that everything syncs.

I got the idea and sample code from the following site: How to synchronize animation through usercontrols in wpf

I made a few changes, but overall, he did exactly what I needed.

+3
source

Obviously this will need to be changed, but trying a duration of 0 when changing colors can give you a blink with less overhead. And this is not so. Just try something.

  <Rectangle Name="MyRectangle" Width="100" Height="100"> <Rectangle.Fill> <SolidColorBrush x:Name="MySolidColorBrush" Color="Blue" /> </Rectangle.Fill> <Rectangle.Triggers> <EventTrigger RoutedEvent="Rectangle.MouseEnter"> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="MyRectangle" Storyboard.TargetProperty="Width" From="100" To="200" Duration="0:0:1" /> <ColorAnimation Storyboard.TargetName="MySolidColorBrush" Storyboard.TargetProperty="Color" From="Blue" To="Red" BeginTime="0:0:1" Duration="0:0:0" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Rectangle.Triggers> </Rectangle> 
0
source

Source: https://habr.com/ru/post/913576/


All Articles