Wpf XAML动画移动倒计时

Wpf XAML动画移动倒计时,wpf,xaml,animation,triggers,Wpf,Xaml,Animation,Triggers,我有以下文本块,它将是一个移动倒计时计时器: <TextBlock x:Name="countdown"> <TextBlock.RenderTransform> <TranslateTransform x:Name="countdownTransform" /> </TextBlock.RenderTransform> </TextBlock> 以下触发器应移动文本块并设置倒计时文

我有以下文本块,它将是一个移动倒计时计时器:

 <TextBlock x:Name="countdown">
      <TextBlock.RenderTransform>
          <TranslateTransform x:Name="countdownTransform" />
      </TextBlock.RenderTransform>
 </TextBlock>

以下触发器应移动文本块并设置倒计时文本:

<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>

但是,DoubleAnimation的From属性(显然)不接受TimeSpan,对于第二个触发器,To属性没有绑定到TextBlock的宽度。是否有一种自定义类型的动画接受TimeSpan at To属性


我希望能够在XAML中实现这一点,我知道这在c代码中是可能的。

文本块的
Width
NaN
,除非您明确设置它,并且
NaN
不是DoubleAnimation的
to
属性的有效值。您应该在VS输出窗口中看到错误消息:

System.Windows.Data错误:5:BindingExpression生成的值为 对目标属性无效。;Value='NaN' BindingExpression:路径=宽度;DataItem='TextBlock'(Name='countdown'); 目标元素为“DoubleAnimation”(HashCode=62632450);目标 属性为“To”(类型为“Nullable`1”)

您可以改为绑定到
实际宽度

<DoubleAnimation ... To="{Binding ElementName=countdown, Path=ActualWidth}" />


对于其他动画:如果持续时间是一个常量,为什么不简单地设置
From=“30”
?否则,您还必须拥有一个ProgressBar,该ProgressBar为
最小值
最大值
等占用时间跨度。

文本块的
宽度
NaN
,除非您明确设置,并且
NaN
不是DoubleAnimation的
属性的有效值。您应该在VS输出窗口中看到错误消息:

System.Windows.Data错误:5:BindingExpression生成的值为 对目标属性无效。;Value='NaN' BindingExpression:路径=宽度;DataItem='TextBlock'(Name='countdown'); 目标元素为“DoubleAnimation”(HashCode=62632450);目标 属性为“To”(类型为“Nullable`1”)

您可以改为绑定到
实际宽度

<DoubleAnimation ... To="{Binding ElementName=countdown, Path=ActualWidth}" />


对于其他动画:如果持续时间是一个常量,为什么不简单地设置
From=“30”
?否则,您还必须有一个ProgressBar,它为
最小值
最大值
等占用时间跨度。

感谢您的回答,实际上持续时间不是一个常量a值,它绑定到一个资源:duration=“{binding XPath=time}”。第一个动画正在运行。为什么不能绑定到like
Duration=“{Binding XPath=time.TotalMinutes}”
?感谢您的回答,实际上Duration不是一个常量a值,它绑定到一个资源:Duration=“{Binding XPath=time}”。第一个动画正在运行。为什么不能绑定到like
Duration=“{Binding XPath=time.TotalMinutes}”