title: c# wpf overlapping controls not receiving mouse events
body: <p>I am building a canvas control. This root canvas has several overlapping children (canvas as well). This is done so each child can handle its own drawing and I can then compose the final result with any combination of children to get the desired behavior.</p>
  
  <p>This is working very well as far as rendering is concerned. This does not work so well with mouse events however. The way mouse events works are as follow (using previewmousemove as an example):</p>
  
  <p>1- If root canvas is under mouse, fire event
  2- Check all children, if one is under mouse, fire event and stop</p>
  
  <p>As such, only the first child I add will receive the mouse move event. The event is not propagated to all children because they overlap.</p>
  
  <p>To overcome this, I attempted the following:
  1- Override mouse events in the root canvas
  2- For every event, find all children that want to handle the event using VisualTreeHelper.HitTest
  3- For all children that returned a valid hit test result (ie: under mouse and willing to handle the event (IsHitTestVisible == true)), ???</p>
  
  <p>This is where I am stuck, I somehow need to send the mouse event to all children, and stop the normal flow of the event to make sure the first child doesn't receive it twice (via handled = true in the event).</p>
  
  <p>By using RaiseEvent with the same event passed on the children, things seem to work but somehow it raises the event on the parent (root canvas) as well. To bypass this I needed to create a copy of the event and set force set the source though it appears to be more of a hack than a solution. Is there a proper way to do what I am trying to do? Code example follows.</p>
  
  <pre><code>    public class CustomCanvas : Canvas
      {
          private List&lt;object&gt; m_HitTestResults = new List&lt;object&gt;();
  
          public new event MouseEventHandler MouseMove;
  
          public CustomCanvas()
          {
              base.PreviewMouseMove += new MouseEventHandler(CustomCanvas_MouseMove);
          }
  
          private void CustomCanvas_MouseMove(object sender, MouseEventArgs e)
          {
  // Hack here, why is the event raised on the parent as well???
              if (e.OriginalSource == this)
              {
                  return;
              }
  
                  Point pt = e.GetPosition((UIElement)sender);
                  m_HitTestResults.Clear();
  
                  VisualTreeHelper.HitTest(this,
                      new HitTestFilterCallback(OnHitTest),
                      new HitTestResultCallback(OnHitTest),
                      new PointHitTestParameters(pt));
  
                  MouseEventArgs tmpe = new MouseEventArgs(e.MouseDevice, e.Timestamp, e.StylusDevice);
                  tmpe.RoutedEvent = e.RoutedEvent;
                  tmpe.Source = this;
  
                  foreach (object hit in m_HitTestResults)
                  {
                      UIElement element = hit as UIElement;
                      if (element != null)
                      {
   // This somehow raises the event on us as well as the element here, why???
                          element.RaiseEvent(tmpe);
                      }
                  }
  
  
              var handlers = MouseMove;
              if (handlers != null)
              {
                  handlers(sender, e);
              }
  
              e.Handled = true;
          }
  
          private HitTestFilterBehavior OnHitTest(DependencyObject o)
          {
              UIElement element = o as UIElement;
              if (element == this)
              {
                  return HitTestFilterBehavior.ContinueSkipSelf;
              }
              else if (element != null &amp;&amp; element.IsHitTestVisible &amp;&amp; element != this)
              {
                  return HitTestFilterBehavior.Continue;
              }
              return HitTestFilterBehavior.ContinueSkipSelfAndChildren;
          }
  
          private HitTestResultBehavior OnHitTest(HitTestResult result)
          {
              // Add the hit test result to the list that will be processed after the enumeration.
              m_HitTestResults.Add(result.VisualHit);
              // Set the behavior to return visuals at all z-order levels.
              return HitTestResultBehavior.Continue;
          }
  </code></pre>