title: DelegateCommand throws "Specified cast is not valid"
body: <p>I want to use a PRISM delegatecommand with a bool as parameter. This is the relevant code:</p>
  
  <pre><code>public class ChartViewModel : BindableBase
  {
      public DelegateCommand&lt;bool?&gt; ChangeZoomPanCommand { get; private set; }
  
      private bool isInRealtimeMode;
      public bool IsInRealtimeMode
      {
          get { return isInRealtimeMode; }
          set
          {
              SetProperty(ref isInRealtimeMode, value);
              ChangeZoomPanCommand.RaiseCanExecuteChanged();
          }
      }
  
      private bool dragToZoom;
      public bool DragToZoom
      {
          get { return dragToZoom; }
          set { SetProperty(ref dragToZoom, value); }
      }
  
  
      public ChartViewModel()
      {
          ChangeZoomPanCommand = new DelegateCommand&lt;bool?&gt;(ExecuteChangeZoomPan, CanExecuteChangeZoomPan);
          IsInRealtimeMode = true;
          DragToZoom = true;
      }
  
      private bool CanExecuteChangeZoomPan(bool? arg)
      {
          return !IsInRealtimeMode;
      }
  
      private void ExecuteChangeZoomPan(bool? enableZoom)
      {
          if (enableZoom.HasValue)
          {
              DragToZoom = enableZoom.Value;
          }
      }
  }
  </code></pre>
  
  <p>When I set a breakpoint in <code>CanExecuteChangeZoomPan</code> it never gets hit. The problem happens after <code>ChangeZoomPanCommand.RaiseCanExecuteChanged()</code>.</p>
  
  <p>This is the stacktrace:</p>
  
  <p>></p>
  
  <pre><code>    at Prism.Commands.DelegateCommand`1.&lt;&gt;c__DisplayClass1_0.&lt;.ctor&gt;b__1(Object o)
     at Prism.Commands.DelegateCommandBase.CanExecute(Object parameter)
     at Prism.Commands.DelegateCommandBase.System.Windows.Input.ICommand.CanExecute(Object parameter)
     at MS.Internal.Commands.CommandHelpers.CanExecuteCommandSource(ICommandSource commandSource)
     at System.Windows.Controls.Primitives.ButtonBase.UpdateCanExecute()
     at System.Windows.Controls.Primitives.ButtonBase.HookCommand(ICommand command)
     at System.Windows.Controls.Primitives.ButtonBase.OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
     at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
     at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
     at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
     at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry&amp; newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
     at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
     at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
     at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
     at System.Windows.Data.BindingExpression.Activate(Object item)
     at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
     at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
     at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
     at MS.Internal.Data.DataBindEngine.Run(Object arg)
     at MS.Internal.Data.DataBindEngine.OnLayoutUpdated(Object sender, EventArgs e)
     at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
     at System.Windows.ContextLayoutManager.UpdateLayout()
     at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
     at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
     at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
     at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
     at System.Windows.Media.MediaContext.AnimatedRenderMessageHandler(Object resizedCompositionTarget)
     at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
     at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
  </code></pre>
  
  <p>If I change the argument type to string everything seems to work. I then of course get a "False" string as argument because that is my xaml commandparameter. Why is this not working with any T as there seems to be no limit on T. I found out the hard way already that T must be an object or nullable, but even nullable doesn't seem to fit. How to work with a bool argument?</p>
  
  <p>Thanks</p>