title: WPF - Declaring a custom routed event and listening to it
body: <p>In short: I want to declare a custom routed event and at the same time listen to it from the same user control that declared it.</p>
  
  <p>What I want to achieve is to have a user control that serves requests for a certain task, so I imagined this scenario:</p>
  
  <ul>
  <li>the user control class declare a custom routed event</li>
  <li>the user control class listen to its own custom routed event through AddHandler(...)</li>
  </ul>
  
  <p>Then:</p>
  
  <ul>
  <li>some random item in the visual tree use RaiseEvent(...) to...well, raise the event.</li>
  <li>an instance of the user control in the tree serves the request.</li>
  </ul>
  
  <p>It doesn't seem to work. It's a bit different from the usual scenario where the user control declare and raise the event, I know, so I did some testing.</p>
  
  <hr>
  
  <p>How to create a custom routed event seems clear, it's not the first time I do it. I have created an example user control, and this it's code behind:</p>
  
  <pre><code>public partial class FuffaControl : UserControl
  {
      public static readonly RoutedEvent FuffaEvent =  
          EventManager.RegisterRoutedEvent("Fuffa", RoutingStrategy.Bubble, 
             typeof(FuffaEventHandler), typeof(FuffaControl));
  
      // Provide CLR accessors for the event
      public event FuffaEventHandler Fuffa
      {
          add { AddHandler(FuffaEvent, value); }
          remove { RemoveHandler(FuffaEvent, value); }
      }
  
      public FuffaControl()
      {
          InitializeComponent();
      }
  }
  </code></pre>
  
  <p>So far, so good. Then, for test purpose, I've declared a window with the custom control inside and a button. This is the content of the window:</p>
  
  <pre><code>&lt;Grid&gt;
      &lt;local:FuffaControl&gt;
          &lt;Grid&gt;
              &lt;Button Content="Fuffa" Click="Button_Click"/&gt;
           &lt;/Grid&gt;
      &lt;/local:FuffaControl&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>In the code behind, I use AddHandler to listen to the event and I raise the event on the click of the button:</p>
  
  <pre><code>public MainWindow()
  {
      InitializeComponent();
      this.AddHandler(FuffaControl.FuffaEvent, new FuffaEventHandler(OnFuffaEvent));
  }
  
  private void Button_Click(object sender, RoutedEventArgs e)
  {
      RoutedEventArgs newEventArgs = new RoutedEventArgs(FuffaControl.FuffaEvent);
      RaiseEvent(newEventArgs);
  }
  
  private void OnFuffaEvent(object sender, RoutedEventArgs e) { }
  </code></pre>
  
  <p>It works. It makes not much sense (I mean, it's not that useful), but I'm using it just to test that the event itself works correctly. Ok, unless C# is doing something weird and cutting a few corners, but at a first glance it seems to me that the button is raising a custom event, the event travels up through the tree (it's a bubbling event, after all), and the window receives it.</p>
  
  <p>So, next I take the AddHandler(...) call and the handler function and move them to the user control; now it's not the window who listen to the Raisevent(...), but the user control itself. If it works, I have children elements raising an event, and the parent user control managing it:</p>
  
  <pre><code>public partial class FuffaControl : UserControl
  {
      public static readonly RoutedEvent FuffaEvent = EventManager.RegisterRoutedEvent("Fuffa", RoutingStrategy.Bubble, typeof(FuffaEventHandler), typeof(FuffaControl));
      // Provide CLR accessors for the event
      public event FuffaEventHandler Fuffa
      {
          add { AddHandler(FuffaEvent, value); }
          remove { RemoveHandler(FuffaEvent, value); }
      }
  
      public FuffaControl()
      {
          InitializeComponent();
  
          this.AddHandler(FuffaControl.FuffaEvent, new FuffaEventHandler(OnFuffaEvent));
      }
  
      private void OnFuffaEvent(object sender, RoutedEventArgs e)
      {
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Nnnnnope. It doesn't work. Why? What's wrong with this? Why the user control cannot listen to its own event?</p>