title: Adding traditional events to a custom control
body: <p>I have been trying to figure this out with lots of googling and SO, but unfortunately I cannot solve this issue. The more I read, the more confused I get.</p>
  <p>I would like to build an autocomplete textbox as a custom control.</p>
  <p>My CustomControl:</p>
  <pre><code>&lt;UserControl x:Class=&quot;ApplicationStyling.Controls.AutoCompleteTextBox&quot;
               xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
               xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
               xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
               xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
               xmlns:local=&quot;clr-namespace:ApplicationStyling.Controls&quot;
               mc:Ignorable=&quot;d&quot;
               d:DesignHeight=&quot;300&quot;
               d:DesignWidth=&quot;300&quot;
               Name=&quot;AutoCompleteBox&quot;&gt;
      &lt;Grid&gt;
          &lt;TextBox Grid.Row=&quot;3&quot;
                   Style=&quot;{DynamicResource InputBox}&quot;
                   x:Name=&quot;SearchBox&quot;
                   Text=&quot;{Binding Text}&quot;
                   TextChanged=&quot;{Binding ElementName=AutoCompleteBox, Path=TextChanged}&quot;/&gt;
  
          &lt;ListBox x:Name=&quot;SuggestionList&quot;
                   Visibility=&quot;Collapsed&quot;
                   ItemsSource=&quot;{Binding ElementName=AutoCompleteTextBox, Path=SuggestionsSource}&quot;
                   SelectionChanged=&quot;{Binding ElementName=AutoCompleteBox, Path=SelectionChanged}&quot;&gt;
              &lt;ListBox.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;Label Content=&quot;{Binding Label}&quot; /&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListBox.ItemTemplate&gt;
          &lt;/ListBox&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  <p>My Code Behind:</p>
  <pre><code>using System.Collections;
  using System.Windows;
  using System.Windows.Controls;
  
  namespace ApplicationStyling.Controls
  {
      /// &lt;summary&gt;
      /// Interaction logic for AutoCompleteTextBox.xaml
      /// &lt;/summary&gt;
      public partial class AutoCompleteTextBox : UserControl
      {
             
          public static readonly DependencyProperty SuggestionsSourceProperty;
          public static readonly DependencyProperty TextProperty;
  
          // Events
          public static readonly RoutedEvent TextChangedProperty;
          public static readonly RoutedEvent SelectionChangedProperty;
  
  
          static AutoCompleteTextBox()
          {
              // Attributes
              AutoCompleteTextBox.SuggestionsSourceProperty = DependencyProperty.Register(&quot;SuggestionsSource&quot;, typeof(IEnumerable), typeof(AutoCompleteTextBox));
              AutoCompleteTextBox.TextProperty = DependencyProperty.Register(&quot;Text&quot;, typeof(string), typeof(AutoCompleteTextBox));
  
              // Events
              AutoCompleteTextBox.TextChangedProperty = EventManager.RegisterRoutedEvent(&quot;TextChanged&quot;, RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AutoCompleteTextBox));
              AutoCompleteTextBox.SelectionChangedProperty = EventManager.RegisterRoutedEvent(&quot;SelectionChanged&quot;, RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(AutoCompleteTextBox));
  
          }
  
          #region Events
          public event RoutedEventHandler TextChanged
          {
              add { AddHandler(TextChangedProperty, value); }
              remove { RemoveHandler(TextChangedProperty, value); }
          }
  
          // This method raises the Tap event
          void RaiseTextChangedEvent()
          {
              RoutedEventArgs newEventArgs = new RoutedEventArgs(AutoCompleteTextBox.TextChangedProperty);
              RaiseEvent(newEventArgs);
          }
  
  
  
          public event RoutedEventHandler SelectionChanged
          {
              add { AddHandler(SelectionChangedProperty, value); }
              remove { RemoveHandler(SelectionChangedProperty, value); }
          }
  
          // This method raises the Tap event
          void RaiseSelectionChangedEvent()
          {
              RoutedEventArgs newEventArgs = new RoutedEventArgs(AutoCompleteTextBox.SelectionChangedProperty);
              RaiseEvent(newEventArgs);
          }
  
          #endregion
  
          #region DProperties
          /// &lt;summary&gt;
          /// IEnumerable ItemsSource Property for the Suggenstion Box
          /// &lt;/summary&gt;
          public IEnumerable SuggestionsSource
          {
              get
              {
                  return (IEnumerable)GetValue(AutoCompleteTextBox.SuggestionsSourceProperty);
              }
              set
              {
                  SetValue(AutoCompleteTextBox.SuggestionsSourceProperty, value);
              }
          }
  
          /// &lt;summary&gt;
          /// This is the Text attribute which routes to the Textbox
          /// &lt;/summary&gt;
          public string Text
          {
              get
              {
                  return (string)GetValue(AutoCompleteTextBox.TextProperty);
              }
              set
              {
                  SetValue(AutoCompleteTextBox.TextProperty, value);
              }
          }
          #endregion
  
          public AutoCompleteTextBox()
          {
              InitializeComponent();
              SearchBox.TextChanged += (sender, args) =&gt; RaiseTextChangedEvent();
              SuggestionList.SelectionChanged += (sender, args) =&gt; RaiseSelectionChangedEvent();
          }
  
  
      }
  }
  </code></pre>
  <p>And lastly, the way I use it:</p>
  <pre><code>&lt;asc:AutoCompleteTextBox x:Name=&quot;ShareAutoCompleteBox&quot;
                                   Grid.Row=&quot;3&quot;
                                   SelectionChanged=&quot;ShareAutoCompleteBox_SelectionChanged&quot;
                                   TextChanged=&quot;ShareAutoCompleteBox_TextChanged&quot;/&gt;
  </code></pre>
  <p>where <code>asc</code> is the namespace for the outsourced class library which is loaded via app.xaml.</p>
  <p>Anyways, the issues I am getting in the XAML at the <code>TextBox.TextChanged</code> attribute, and when running the code:</p>
  <blockquote>
  <p>System.InvalidCastException: Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'.</p>
  </blockquote>
  <p>So what exactly is going on here? I would like to forward the <code>AutoCompleteTextBox TextChanged</code> to the <code>TextBox</code> within the Custom Control Template. Same with the <code>SelectionChanged</code> to the <code>Listbox</code>.</p>
  <p>I took most of the code from either <a href="https://msdn.microsoft.com/en-us/library/ms752288(v=vs.100).aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/ms752288(v=vs.100).aspx</a> (for the events) and from some other SO questions the code for the custom properties.</p>
  <p>Not sure, what the problem is and I am looking forward to your help.</p>