title: Command binding Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'
body: <p>When I wire up my button to a command via XAML, I'm getting a run time error 
  System.Windows.Markup.XamlParseException: Provide value on 'System.Windows.Data.Binding' threw an exception. ---> System.InvalidCastException: Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'.</p>
  
  <p>When I remove the command binding in XAML everything works okay, and my items display etc..
  Here is the command binding:</p>
  
  <pre><code>Click="{Binding ElementName=MainGrid, Path=DataContext.AlertClickCommand}"
  </code></pre>
  
  <p>Code to wire up view model (in code behind of my window):</p>
  
  <pre><code>this.AlertsView.DataContext = GlobalStuff.AlertManager1.AlertViewModel1;
  </code></pre>
  
  <p><strong>Here is my view Model</strong> My view model is the data context of my view</p>
  
  <pre><code>using System.Collections.Generic;
  using System.ComponentModel;
  using Arkle.SharedUI.Model;
  using Arkle.SharedUI.ViewModel.Commands;
  
  namespace Arkle.SharedUI.ViewModel
  {
      public class AlertViewModel : INotifyPropertyChanged
      {
  
          private List&lt;Alert&gt; _alerts = new List&lt;Alert&gt;();
          public List&lt;Alert&gt; Alerts
          {
              get { return _alerts; }
              set
              {
                  _alerts = value;
                  OnPropertyChanged("Alerts");
              }
          }
  
  
          public AlertViewModel()
          {
              if (DesignerProperties.IsInDesignMode)
              {
                  LoadDesignTimeData();
              }
          }
  
          private void LoadDesignTimeData()
          {
              Alerts.Add(new Alert { BackgroundMessage = "Sis", IsAlerting = true, OverlayMessage = "3 mins", Tip = "Sis Data not received for 3 mins" });
              Alerts.Add(new Alert { BackgroundMessage = "Bets", IsAlerting = true, OverlayMessage = "4", Tip = "4 unchecked danger bets" });
              Alerts.Add(new Alert { BackgroundMessage = "Texts", IsAlerting = false, OverlayMessage = "3", Tip = "3 Unchecked Text Bets" });
          }
  
          private AlertClickCommand _alertClickCommand;
          public AlertClickCommand AlertClickCommand
          {
              get { return _alertClickCommand ?? (_alertClickCommand = new AlertClickCommand(this)); }
          }
          public event PropertyChangedEventHandler PropertyChanged;
  
          protected virtual void OnPropertyChanged(string propertyName)
          {
              PropertyChangedEventHandler handler = PropertyChanged;
              if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  }
  </code></pre>
  
  <p><strong>Here is my xaml</strong></p>
  
  <pre><code>&lt;UserControl x:Class="Arkle.SharedUI.View.AlertsView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:viewModel="clr-namespace:Arkle.SharedUI.ViewModel"
          mc:Ignorable="d"
          d:DataContext="{d:DesignInstance Type=viewModel:AlertViewModel, IsDesignTimeCreatable=True}"
          x:Name="EarlyPriceEditorViewModelWindow"
      Height="Auto" Width="Auto"&gt;
      &lt;UserControl.Resources&gt;
          &lt;BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/&gt;
      &lt;/UserControl.Resources&gt;
  
      &lt;Grid Name="MainGrid"&gt;
          &lt;StackPanel Name="MainStackPanel"&gt;
              &lt;ListBox   ItemsSource="{Binding Path=Alerts}"   &gt;
                  &lt;ListBox.ItemsPanel&gt;
                      &lt;ItemsPanelTemplate&gt;
                          &lt;WrapPanel Orientation="Horizontal" &gt;
                          &lt;/WrapPanel&gt;
                      &lt;/ItemsPanelTemplate&gt;
                  &lt;/ListBox.ItemsPanel&gt;
  
                  &lt;ListBox.ItemTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;StackPanel Orientation="Horizontal" Visibility="{Binding IsAlerting,Converter={StaticResource BooleanToVisibilityConverter}}"&gt;
                              &lt;StackPanel Orientation="Horizontal"&gt;
                                  &lt;Button Content="{Binding BackgroundMessage}" HorizontalAlignment="Left"  Width="75"  VerticalAlignment="Top" Height="Auto"  Margin="2" 
                                      Click="{Binding ElementName=MainGrid, Path=DataContext.AlertClickCommand}" CommandParameter="{Binding}"
  
                                   /&gt;
                                  &lt;Label Content="{Binding OverlayMessage}" HorizontalAlignment="Left" Width="Auto" Margin="1,0,0,0" VerticalAlignment="Top" Background="Red" Foreground="White"
                                     FontWeight="Bold"&gt;
                                      &lt;Label.Style&gt;
                                          &lt;Style&gt;
                                              &lt;Style.Triggers&gt;
                                                  &lt;DataTrigger Binding="{Binding IsAlerting}" Value="True"&gt;
                                                      &lt;Setter  Property="Image.Visibility" Value="Visible" /&gt;
                                                      &lt;DataTrigger.EnterActions&gt;
                                                          &lt;BeginStoryboard x:Name="ImageFlash"&gt;
                                                              &lt;Storyboard&gt;
                                                                  &lt;DoubleAnimation Storyboard.TargetProperty="(UIElement.Opacity)"
                                                  BeginTime="0:0:0" Duration="0:0:0.5"
                                                  From="1.0" To="0.0" RepeatBehavior="Forever" AutoReverse="True"/&gt;
                                                              &lt;/Storyboard&gt;
                                                          &lt;/BeginStoryboard&gt;
                                                      &lt;/DataTrigger.EnterActions&gt;
                                                      &lt;DataTrigger.ExitActions&gt;
                                                          &lt;StopStoryboard BeginStoryboardName="ImageFlash" /&gt;
                                                      &lt;/DataTrigger.ExitActions&gt;
                                                  &lt;/DataTrigger&gt;
                                                  &lt;DataTrigger Binding="{Binding IsAlerting}" Value="False"&gt;
                                                      &lt;DataTrigger.Setters&gt;
                                                          &lt;Setter  Property="Image.Visibility" Value="Collapsed" /&gt;
                                                      &lt;/DataTrigger.Setters&gt;
                                                  &lt;/DataTrigger&gt;
                                              &lt;/Style.Triggers&gt;
                                          &lt;/Style&gt;
  
                                      &lt;/Label.Style&gt;
                                  &lt;/Label&gt;
                                  &lt;Label Content="|" Margin="5"/&gt;
                              &lt;/StackPanel&gt;
  
  
                          &lt;/StackPanel&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/ListBox.ItemTemplate&gt;
  
              &lt;/ListBox&gt;
  
  
          &lt;/StackPanel&gt;
      &lt;/Grid&gt;
  
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>Here is my command</strong></p>
  
  <pre><code>using System;
  using System.Windows.Input;
  using Arkle.Common;
  using Arkle.SharedUI.Events;
  using Arkle.SharedUI.Model;
  
  namespace Arkle.SharedUI.ViewModel.Commands
  {
      public class AlertClickCommand : ICommand
      {
          private AlertViewModel _alertViewModel;
          public AlertClickCommand(AlertViewModel alertViewModel)
          {
              _alertViewModel = alertViewModel;
          }
  
          public void Execute(object parameter)
          {
              if (parameter == null)
              {
                  return;
              }
              var parameterAsAlert = (Alert)parameter;
  
              switch (parameterAsAlert.BackgroundMessage)
              {
                  case "Bets":
                      EventManager.Instance.GetEvent&lt;ShowDangerBetsRequestedEvent&gt;().Publish(null);
                      break;
                  default:
                      return;
              }
          }
  
          public bool CanExecute(object parameter)
          {
              return true;
          }
  
          public event EventHandler CanExecuteChanged;
      }
  }
  </code></pre>
  
  <p>I also get the following design time error (see screenshot)
  Unable to cast object of type 'System.Windows.Data.Binding' to type 'Microsoft.Expression.Markup.DocumentModel.DocumentNode'.</p>
  
  <p><img src="https://i.stack.imgur.com/sKw41.png" alt="enter image description here"></p>
  
  <p><strong>First Runtime Error - Throws Run</strong>
  <img src="https://i.stack.imgur.com/SUrkq.png" alt="enter image description here"></p>
  
  <p><strong>Subsequent runtime errors throws repeatedly:</strong>
  System.Windows.Markup.XamlParseException: Provide value on 'System.Windows.Data.Binding' threw an exception. ---> System.InvalidCastException: Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'.</p>
  
  <p>at MS.Internal.Helper.CheckCanReceiveMarkupExtension(MarkupExtension markupExtension, IServiceProvider serviceProvider, DependencyObject&amp; targetDependencyObject, DependencyProperty&amp; targetDependencyProperty)</p>
  
  <p>at System.Windows.Data.BindingBase.ProvideValue(IServiceProvider serviceProvider)</p>
  
  <p>at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(MarkupExtension me, IServiceProvider serviceProvider)</p>
  
  <p>--- End of inner exception stack trace ---</p>
  
  <p>at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)</p>
  
  <p>.......................</p>