I'm creating a custom control in WPF
which actually is an AnalogClock
. This is a very simple class with a template as shown below:
public class AnalogClock {
public static readonly DependencyProperty AnimatorProperty
= DependencyProperty.Register("Animator", typeof(Storyboard), typeof(AnalogClock),
new FrameworkPropertyMetadata(null));
public Storyboard Animator {
get { return (Storyboard)GetValue(AnimatorProperty); }
set { SetValue(AnimatorProperty, value); }
}
public override void OnApplyTemplate() {
base.OnApplyTemplate();
if (Animator == null)
return;
ApplyTargetToTimeline("PART_HourHandAnimator", "PART_HourHand");
ApplyTargetToTimeline("PART_MinuteHandAnimator", "PART_MinuteHand");
ApplyTargetToTimeline("PART_SecondHandAnimator", "PART_SecondHand");
ApplyTargetToTimeline("PART_IconAnimator", "PART_Icon");
Animator.Begin(this, Template);
// try to seek the timeline, to DateTime.Now.TimeOfDay
// but can not ):
Animator.Seek(this, DateTime.Now.TimeOfDay, TimeSeekOrigin.Duration);
}
private void ApplyTargetToTimeline(string timelineName, string targetName) {
var timeline = Animator.Children.FirstOrDefault(t => t.Name == timelineName);
if (timeline == null)
throw new InvalidOperationException(string.Format(
"The Timeline named '{0}' could not be found.", timelineName));
Storyboard.SetTargetName(timeline, targetName);
}
}
And here is the XAML
:
The Storyboard resource:
<Storyboard x:Key="Animator">
<DoubleAnimation x:Name="PART_HourHandAnimator" From="0" To="360" Duration="12:00:00" BeginTime="00:00:00.000000"
RepeatBehavior="Forever" Storyboard.TargetName="{Binding}" Storyboard.Target="{Binding}"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"/>
<DoubleAnimation x:Name="PART_MinuteHandAnimator" From="0" To="360" Duration="1:00:00" BeginTime="00:00:00.000000"
RepeatBehavior="Forever" Storyboard.TargetName="{Binding}" Storyboard.Target="{Binding}"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"/>
<DoubleAnimation x:Name="PART_SecondHandAnimator" From="0" To="360" Duration="00:01:00" BeginTime="00:00:00.000000"
RepeatBehavior="Forever" Storyboard.TargetName="{Binding}" Storyboard.Target="{Binding}"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"/>
<DoubleAnimation x:Name="PART_IconAnimator" From="0" To="360" Duration="00:01:00" BeginTime="00:00:00.000000"
RepeatBehavior="Forever" Storyboard.TargetName="{Binding}" Storyboard.Target="{Binding}"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"/>
</Storyboard>
The Template:
<ControlTemplate x:Key="MyTemplate" TargetType="{x:Type ui:AnalogClock}">
<Border>
<Viewbox>
<Grid>
<Ellipse x:Name="PART_Icon" >
<Ellipse.RenderTransform>
<RotateTransform CenterX="25" CenterY="25" />
</Ellipse.RenderTransform>
</Ellipse>
<Grid>
<Canvas x:Name="PART_SecondHand">
<Ellipse RenderTransformOrigin="0.5,0.5"/>
<Canvas.RenderTransform>
<RotateTransform CenterX="60" CenterY="60"/>
</Canvas.RenderTransform>
</Canvas>
<Canvas>
<Path x:Name="PART_HourHand" Fill="SomeBrush"
Data="Some data" RenderTransformOrigin="0.493,0.933">
<Path.RenderTransform>
<RotateTransform />
</Path.RenderTransform>
</Path>
<Path x:Name="PART_MinuteHand" Fill="Some brush"
Data="Some Data" RenderTransformOrigin="0.493,0.933">
<Path.RenderTransform>
<RotateTransform />
</Path.RenderTransform>
</Path>
</Canvas>
</Grid>
</Grid>
</Viewbox>
</Border>
</ControlTemplate>
The Style:
<Style x:Key="MyClock" TargetType="{x:Type ui:AnalogClock}">
<Setter Property="Animator" Value="{StaticResource Animator}"/>
<Setter Property="Template" Value="{StaticResource MyTemplate}"/>
</Style>
As you can see it's a really really simple control. Well, it works just fine except the storyboard.Seek
method doesn't affect at all. I mean, the control shown well, the animations begin as well as I want, but from the zero time ); I wish I could bring the position of all timelines to DateTime.Now.TimeOfDay
, but I can't. I tried TimeSeekOrigin.Duration
and TimeSeekOrigin.BeginTime
. But none of them work. Can you help to find the problem? Thanks in advance.
Finally, I got lucky :) First of all, when we begin a Storyboard
on a Template
, it's not needed to set target name. Second, to Storyboard.Seek
take effect, we should begin the storyboard with isControllable = true
parameter. Together:
<Storyboard x:Key="Animator">
<DoubleAnimation From="0" To="360" Duration="12:00:00"
RepeatBehavior="Forever" Storyboard.TargetName="PART_HourHand"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"/>
<DoubleAnimation From="0" To="360" Duration="1:00:00"
RepeatBehavior="Forever" Storyboard.TargetName="PART_MinuteHand"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"/>
<DoubleAnimation From="0" To="360" Duration="00:01:00"
RepeatBehavior="Forever" Storyboard.TargetName="PART_SecondHand"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"/>
<DoubleAnimation From="0" To="360" Duration="00:01:00"
RepeatBehavior="Forever" Storyboard.TargetName="PART_Icon"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"/>
</Storyboard>
And the beginner code:
public override void OnApplyTemplate() {
base.OnApplyTemplate();
if (Animator == null)
return;
// begin the storyboard on `this` control with its own `Template` and controllable
Animator.Begin(this, Template, true);
// the storyboard has a duration of Forever -as you can see, we did not specified any duration. Is this a default behavior?
// while the storyboard has a duration of Forever, we have to set seekOrigin to BeginTime
Animator.Seek(this, DateTime.Now.TimeOfDay, TimeSeekOrigin.BeginTime);
}