2012-03-15 65 views
1

我有一个使用CM的WPF项目。我有一个进度条,我想顺利进行动画制作。我有一个包含DoubleAnimation的故事板。问题是,当我尝试将DoubleAnimation的持续时间绑定到视图模型上的属性时,运行该程序时出现TargetInvocationException。绑定到DoubleAnimation的持续时间抛出TargetInvocationException

进度条的XAML看起来是这样的:

<ProgressBar Name="ProgressBar" Width="400" Height="18"> 
    <ProgressBar.Style> 
     <Style> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding Path=IsMeasuring}" Value="True"> 
        <DataTrigger.EnterActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <DoubleAnimation 
            Storyboard.Target="{Binding TemplatedParent}" 
            Storyboard.TargetProperty="Value" 
            From="0" 
            To="100" 
            Duration="{Binding MeasurementDuration}" 
           /> 
          </Storyboard> 
         </BeginStoryboard> 
        </DataTrigger.EnterActions> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </ProgressBar.Style> 
</ProgressBar> 

虽然在我看来模型的属性看起来是这样的:

private Duration measurementDuration = new Duration(TimeSpan.FromSeconds(1)); 
public Duration MeasurementDuration 
{ 
    get { return measurementDuration; } 
    private set 
    { 
     measurementDuration = value; 
     NotifyOfPropertyChange(() => MeasurementDuration); 
    } 
} 

我觉得答案是直盯着我,但我根本找不到它。预先感谢您的帮助。

回答

1

据我所知,在这种实现方式下绑定到一个动画,因为你的抛出一个错误。 如果在动画时更改Timeline -Object的属性,则必须使用方法<StoryboardName>.Begin()Storyboard<StoryboardName>.Seek()创建一个新时钟以跳转到达到的持续时间。

在您调用<name>.Begin()方法之前,您需要更改所需的属性并且它应该可以工作。

他们的存在在MSDN一个例子: http://archive.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=wpfsamples&DownloadId=7734

事实上,在MSDN例如“唯一”的KeyFrames一个一个TimeLine -object的-objects将被改变,你必须调用Storyboard.Stop()方法在更改Duration属性之前。

试试吧,也许它可以帮助:)

亲切的问候 锑

+0

的事情是,我并不想在动画运行时更改时间。我想要做的是使用绑定设置动画的持续时间,然后再开始动画。这一定是可能的,但我尝试过的组合没有奏效。 – 2012-03-16 09:59:13

+0

嘿。你得到的例外不是持续时间绑定。问题是Storyboard.TargetName。除了模板之外,您不得在样式中使用Storyboard.TargetName。 http://msdn.microsoft.com/en-us/library/ms742868.aspx(#地区风格的动画)。尝试你的解决方案,而不设置Storyboard.TargetName属性,它应该工作。 – Shounbourgh 2012-03-16 10:38:01

+0

我不认为这是问题所在。它可以与Storyboard.Target集合和静态持续时间一起工作。只要我添加持续时间,它就停止工作。感谢您的链接,但似乎很有用。 – 2012-03-16 11:17:35