title: The correct way to do a tunneled event
body: <p><strong>EDIT</strong>: I guess I asked a bit of a XY Problem. I don't really care about getting tunneled events working, what I care about is getting a event <strong><em>raised from the code behind of the parent window</em></strong> to be picked up and reacted to by a control that is a child of that window without explicitly needing to tell the child who its parent is and manually subscribing to the event.</p>
  
  <hr>
  
  <p>I am trying to raise a event in a parent control and having the child controls listen for that event and react to it. From my research I thought I just needed to do a <code>RoutedEvent</code> but I am doing something incorrect.</p>
  
  <p>Here is a MCVE showing what I have tried, it is a simple program with a window and a UserControl inside of it.</p>
  
  <pre><code>&lt;Window x:Class="RoutedEventsTest.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:local="clr-namespace:RoutedEventsTest"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto"/&gt;
              &lt;RowDefinition/&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;Button Name="button" Click="ButtonBase_OnClick" HorizontalAlignment="Left" 
                  VerticalAlignment="Top"&gt;Unhandled in parent&lt;/Button&gt;
          &lt;local:ChildControl Grid.Row="1"/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  
  
  <pre><code>using System.Windows;
  
  namespace RoutedEventsTest
  {
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
              TestEventHandler += MainWindow_TestEventHandler;
          }
  
          void MainWindow_TestEventHandler(object sender, RoutedEventArgs e)
          {
              button.Content = "Handeled in parent";
              e.Handled = false;
          }
  
          private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
          {
              RaiseEvent(new RoutedEventArgs(TestEvent));
          }
  
          public static readonly RoutedEvent TestEvent = EventManager.RegisterRoutedEvent("TestEvent", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(MainWindow));
  
          public event RoutedEventHandler TestEventHandler
          {
              add { AddHandler(TestEvent, value); }
              remove { RemoveHandler(TestEvent, value); }
          }
      }
  }
  </code></pre>
  
  
  
  <pre><code>&lt;UserControl x:Class="RoutedEventsTest.ChildControl"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
      &lt;Grid&gt;
            &lt;TextBlock Name="textBlock"&gt;Unhandeled in child&lt;/TextBlock&gt;  
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  
  
  <pre><code>using System.Windows;
  using System.Windows.Controls;
  
  namespace RoutedEventsTest
  {
      public partial class ChildControl : UserControl
      {
          public ChildControl()
          {
              InitializeComponent();
              AddHandler(MainWindow.TestEvent, new RoutedEventHandler(TestEventHandler));
          }
  
          private void TestEventHandler(object sender, RoutedEventArgs routedEventArgs)
          {
              textBlock.Text = "Handled in child";
              routedEventArgs.Handled = false;
          }
      }
  }
  </code></pre>
  
  <p>When I run the program the parent window reacts like I expect, but the child UserControl never runs its delegate that I passed in to <code>AddHandler</code>.</p>
  
  <p>Changing the child control to be </p>
  
  <pre><code>public partial class ChildControl : UserControl
  {
      public ChildControl()
      {
          InitializeComponent();
          AddHandler(TestEvent, new RoutedEventHandler(TestEventHandler));
      }
  
      public static readonly RoutedEvent TestEvent = EventManager.RegisterRoutedEvent("TestEvent", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(ChildControl));
  
      private void TestEventHandler(object sender, RoutedEventArgs routedEventArgs)
      {
          textBlock.Text = "Handled in child";
          routedEventArgs.Handled = false;
      }
  }
  </code></pre>
  
  <p>did not fix the issue either. I searched a lot and found many examples of how to do a bubbled event going from a child to the parent, but I could not find a single full example showing how to do a tunneled event from a parent to a child.</p>