I have the following TextBlock which will be a moving countdown timer:
<TextBlock x:Name="countdown">
<TextBlock.RenderTransform>
<TranslateTransform x:Name="countdownTransform" />
</TextBlock.RenderTransform>
</TextBlock>
The following triggers should move the TextBlock and set the countdown text:
<Grid.Triggers>
<EventTrigger SourceName="PlayButton" RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="songProgressBar"
Storyboard.TargetProperty="Value"
From="0:30:0" To="0" Duration="0:30:0" />
<DoubleAnimation Storyboard.TargetName="countdownTransform"
Storyboard.TargetProperty="X" AutoReverse="True"
From="0.0" To="{Binding ElementName=countdown, Path=Width}" Duration="0:30:0" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
However, the DoubleAnimation’s From property doesn’t accept (obviously) TimeSpan and for the second trigger, To property isn’t binding to the TextBlock’s Width. Is there a custom type of animation that accepts TimeSpan at To property?
I want to be able to do this in XAML, I know this is possible in c# code.
The
Widthof the TextBlock isNaNunless you explicitly set it, andNaNis not a valid value for theToproperty of a DoubleAnimation. You should have seen the error message in VS output window:You could instead bind to
ActualWidth:For the other animation: If the duration is a constant value, why not simply set
From="30"? Otherwise you would also have to have a ProgressBar that takes a TimeSpan forValue,Minimum,Maximumetc.