title: Attached Behavior handling an Attached Event in WPF
body: <p>I googled regarding this question but couldn't gather any information and I was wondering if it is possible for an attached behavior to handle an attached event??</p>
  
  <p>I've an event declared in a class and a behavior that I am attaching to a TextBox control, the event will be raised when a button is clicked. I added the handler for this event in my behavior and wrote the logic in the event handler, but it is not executed. So, I was wondering if it is possible for an attached behavior to handle an attached event or not?</p>
  
  <pre class="lang-cs prettyprint-override"><code>class ResetInputEventClass
  {
      public static readonly RoutedEvent ResetInputEvent =  EventManager.RegisterRoutedEvent("ResetInput",
        RoutingStrategy.Bubble,
        typeof(RoutedEventHandler),
        typeof(ResetInputEventClass));
  
      public static void AddResetInputEventHandler(DependencyObject d, RoutedEventHandler handler)
      {
          UIElement uie = d as UIElement;
          if (uie == null)
          {
              return;
          }
          uie.AddHandler(ResetInputEventClass.ResetInputEvent, handler);
      }
  
      public static void RemoveResetInputEventHandler(DependencyObject d, RoutedEventHandler handler)
      {
          UIElement uie = d as UIElement;
          if (uie == null)
          {
              return;
          }
          uie.RemoveHandler(ResetInputEventClass.ResetInputEvent, handler);
      }
  }
  </code></pre>
  
  <p>That is my Event class and this is how I am handling it in the behavior</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class MyBehavior : Behavior&lt;TextBoxBase&gt;
  {
      public MyBehavior()
      {
          // Insert code required on object creation below this point.
  
      }        
  
      protected override void OnAttached()
      {
          base.OnAttached();
          // Insert code that you would want run when the Behavior is attached to an object.
          ResetInputEventClass.AddResetInputEventHandler(AssociatedObject, OnResetInputEvent);
      }
  
      protected override void OnDetaching()
      {
          base.OnDetaching();
          // Insert code that you would want run when the Behavior is removed from an object.
          ResetInputEventClass.RemoveResetInputEventHandler(AssociatedObject, OnResetInputEvent);
      }
  
      private void OnResetInputEvent(Object o, RoutedEventArgs e)
      {
          //Logic
      }
  }
  </code></pre>
  
  <p>Here is my XAML Code:</p>
  
  <pre><code>&lt;Grid x:Name="LayoutRoot"&gt;
      &lt;StackPanel&gt;
          &lt;TextBox Margin="5" Text="Bye" TextWrapping="Wrap" Width="150"&gt;
              &lt;i:Interaction.Behaviors&gt;
                  &lt;local:MyBehavior/&gt;
              &lt;/i:Interaction.Behaviors&gt;
          &lt;/TextBox&gt;
          &lt;TextBox Margin="5" Text="Bye" TextWrapping="Wrap" Width="150"&gt;
              &lt;i:Interaction.Behaviors&gt;
                  &lt;local:MyBehavior/&gt;
              &lt;/i:Interaction.Behaviors&gt;
          &lt;/TextBox&gt;
          &lt;Button Name="MyButton" Content="Save" Width="50" Height="25" Click="MyButton_Click"/&gt;
      &lt;/StackPanel&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>and I am raising the event in the click event of my button</p>
  
  <pre class="lang-cs prettyprint-override"><code>private void MyButton_Click(object sender, RoutedEventArgs e)
  {
      RoutedEventArgs eventArgs = new RoutedEventArgs(ResetInputEventClass.ResetInputEvent,e.OriginalSource);
      RaiseEvent(eventArgs);
  }
  </code></pre>