Binding to a storyboard duration leads to TargetInvocationException

Topics: Getting Started
Mar 15, 2012 at 12:47 PM
Edited Mar 16, 2012 at 10:02 AM

Hello.

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" Visibility="{Binding IsMeasuring, Converter={StaticResource BooleanToVisibilityConverter}}" 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);
  }
}

The call stack looks like this:

  Caliburn.Micro.dll!Caliburn.Micro.ViewLocator.InitializeComponent(object element) Line 420 + 0xf bytes C#
  Caliburn.Micro.dll!Caliburn.Micro.ViewLocator..cctor.AnonymousMethod__2(System.Type viewType) Line 266 C#
  Caliburn.Micro.dll!Caliburn.Micro.ViewLocator..cctor.AnonymousMethod__d(System.Type modelType, System.Windows.DependencyObject displayLocation, object context) Line 361 + 0x24 bytes C#
  Caliburn.Micro.dll!Caliburn.Micro.ViewLocator..cctor.AnonymousMethod__e(object model, System.Windows.DependencyObject displayLocation, object context) Line 392 + 0x29 bytes C#
  Caliburn.Micro.dll!Caliburn.Micro.View.OnModelChanged(System.Windows.DependencyObject targetLocation, System.Windows.DependencyPropertyChangedEventArgs args) Line 210 + 0x27 bytes C#
  [External Code]
  Caliburn.Micro.dll!Caliburn.Micro.ConventionManager..cctor.AnonymousMethod__3(System.Type viewModelType, string path, System.Reflection.PropertyInfo property, System.Windows.FrameworkElement element, Caliburn.Micro.ElementConvention convention, System.Windows.DependencyProperty bindableProperty) Line 111 + 0xe bytes C#
  Caliburn.Micro.dll!Caliburn.Micro.ConventionManager.SetBindingWithoutBindingOverwrite(System.Type viewModelType, string path, System.Reflection.PropertyInfo property, System.Windows.FrameworkElement element, Caliburn.Micro.ElementConvention convention, System.Windows.DependencyProperty bindableProperty) Line 325 C#
  Caliburn.Micro.dll!Caliburn.Micro.ElementConvention..ctor.AnonymousMethod__0(System.Type viewModelType, string path, System.Reflection.PropertyInfo property, System.Windows.FrameworkElement element, Caliburn.Micro.ElementConvention convention) Line 35 + 0x4a bytes C#
  Caliburn.Micro.dll!Caliburn.Micro.ViewModelBinder..cctor.AnonymousMethod__0(System.Collections.Generic.IEnumerable<System.Windows.FrameworkElement> namedElements, System.Type viewModelType) Line 78 + 0x4f bytes C#
  Caliburn.Micro.dll!Caliburn.Micro.ViewModelBinder..cctor.AnonymousMethod__3(object viewModel, System.Windows.DependencyObject view, object context) Line 196 + 0x14 bytes C#  Caliburn.Micro.dll!Caliburn.Micro.WindowManager.CreateWindow(object rootModel, bool isDialog, object context, System.Collections.Generic.IDictionary<string,object> settings) Line 143 C#
  Caliburn.Micro.dll!Caliburn.Micro.WindowManager.ShowWindow(object rootModel, object context, System.Collections.Generic.IDictionary<string,object> settings) Line 75 + 0x15 bytes C#
  Caliburn.Micro.dll!Caliburn.Micro.Bootstrapper.DisplayRootViewFor(System.Type viewModelType) Line 212 C#
  Caliburn.Micro.dll!Caliburn.Micro.Bootstrapper<GlucoBeam.IShell>.OnStartup(object sender, System.Windows.StartupEventArgs e) Line 233 C#
  [External Code]

Thanks in advance for any help!