I have a WPF project using CM. I have a progress bar that I would like to animate smoothly. I have a storyboard containing a DoubleAnimation. The problem is that when I try to bind the Duration of the DoubleAnimation to a property on my view model, I get a TargetInvocationException on running the program.
The XAML for the progress bar looks like this:
<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>
While the property in my view model looks like this:
private Duration measurementDuration = new Duration(TimeSpan.FromSeconds(1));
public Duration MeasurementDuration
{
get { return measurementDuration; }
private set
{
measurementDuration = value;
NotifyOfPropertyChange(() => MeasurementDuration);
}
}
I feel that the answer is staring straight at me, but I simply cannot find it. Thanks in advance for any help.
as far as i know, a binding to an animation in this implementation way as yours throws always an error.
In the case to change properties of an
Timeline-Object while it is animated, you have to use the methods<StoryboardName>.Begin()to create a new clock for theStoryboardand<StoryboardName>.Seek()to jump to the reached duration.Before you call the
<name>.Begin()method, you change your desired properties and it should work.Their exists a example in the msdn:
http://archive.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=wpfsamples&DownloadId=7734
In fact, that in the msdn example “only” the
KeyFrames-objects of a anTimeLine-Object will be changed, you have to call theStoryboard.Stop()method before you change theDuration-property.Try it and maybe it helps 🙂
Kind regards
Sb