title: MouseButtonEventArgs.MouseDevice.DirectlyOver misunderstanding
body: <p>I was faced with the next misunderstanding.</p>
  
  <p><strong>Preamble</strong>:</p>
  
  <p>I have wpf application with next essential UI parts: <code>RadioButtons</code> and some control that use dropdown based on <code>Popup</code> (in combobox manner). According to some logic every radiobutton hook <code>PreviewMouseDown</code> event and do some calculations. 
  In the next scenario,</p>
  
  <ol>
  <li>User opens popup (do not select something, popup just staying open)</li>
  <li>User click on radiobutton</li>
  </ol>
  
  <p><code>PreviewMouseDown</code> will not be fired for radiobutton as expected (because of <code>Popup</code> <a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/9337ff73-0641-4336-a65b-5aeb3a1ad3d8/" rel="nofollow">feature</a>).</p>
  
  <p>And my aim is firing <code>PreviewMouseDown</code> for <code>RadioButton</code> despite of one.</p>
  
  <p><strong>Attempts to solve</strong>:</p>
  
  <p>Fast and dirty solution is: hook <code>PreviewMouseDown</code> for <code>Popup</code> and re-fire <code>PreviewMouseDown</code> event with new source if required, using radiobutton as source. New source can be obtained via <code>MouseButtonEventArgs.MouseDevice.DirectlyOver</code>. The next piece of code do that (event is re-fired only if <code>Popup</code> "eat" <code>PreviewMouseDown</code> for outer click):</p>
  
  <pre><code>    private static void GrantedPopupPreviewMouseDown(object sender, MouseButtonEventArgs e)
      {
          var popup = sender as Popup;
          if(popup == null)
              return;
  
          var realSource = e.MouseDevice.DirectlyOver as FrameworkElement;
          if(realSource == null || !realSource.IsLoaded)
              return;
  
          var parent = LayoutTreeHelper.GetParent&lt;Popup&gt;(realSource);
  
          if(parent == null || !Equals(parent, popup ))
          {
              e.Handled = true;
              var args = new MouseButtonEventArgs(e.MouseDevice,
                                                  e.Timestamp,
                                                  e.ChangedButton)
              {
                  RoutedEvent = UIElement.PreviewMouseDownEvent,
                  Source = e.MouseDevice.DirectlyOver,
              };
              realSource.RaiseEvent(args);
          }
      }
  </code></pre>
  
  <p>This <strong>works well</strong> when I'm attaching that handler to <code>Popup.PreviewMouseDown</code> directly via <code>Behavior</code> and <strong>do not work</strong> (<code>PreviewMouseDown</code> isn't fired for radiobutton) if I'm attaching one via <code>EventManager.RegisterClassHandler</code> (aim is to avoid attaching behavior to every <code>Popup</code> that can occure on page with these radiobuttons):</p>
  
  <pre><code>EventManager.RegisterClassHandler(
              typeof (Popup),
              PreviewMouseDownEvent,
              new MouseButtonEventHandler(GrantedPopupPreviewMouseDown));
  </code></pre>
  
  <p>Debugger showed that <code>e.MouseDevice.DirectlyOver</code> (see code above) is <code>Popup</code>, not <code>Radiobutton</code> (as it is was when I've attached handler via <code>Behavior</code>)!</p>
  
  <p><strong>Question</strong>:</p>
  
  <p>How and why<code>MouseButtonEventArgs</code> can be different for the same action, if eventhandler attached in two different ways?</p>
  
  <p>Can someone explaing this behavior?</p>
  
  <p>Thanks a lot.</p>