title: How can a control handle a Mouse click outside of that control?
body: <p>I'm writing a custom control and I'd like the control to switch from an editing state to it's normal state when a user clicks off of the control. I'm handling the LostFocus event and that helps when a user tabs away or if they click on to another control that's Focusable. But if they don't click on something Focusable, it won't switch out of it's editing state. So I have two solutions in mind:</p>
  
  <ul>
  <li>Walk up the tree to the top most element when it goes in to an editing state and add a handler for <code>MouseDownEvent</code> (and handle "handled" events). In the handler I'd kick the control out of it's editing state and remove the handler from the top most element. This seems like a bit of a hack, but it would probably work well.</li>
  </ul>
  
  <p>Example code:</p>
  
  <pre><code>private void RegisterTopMostParentMouseClickEvent()
  {
     _topMostParent = this.FindLastVisualAncestor&lt;FrameworkElement&gt;();
     if ( _topMostParent == null )
        return;
     _topMostParent.AddHandler( Mouse.MouseDownEvent, new MouseButtonEventHandler( CustomControlMouseDownEvent ), true );
  }
  
  private void UnRegisterTopMostParentMouseClickEvent()
  {
     if ( _topMostParent == null )
        return;
     _topMostParent.RemoveHandler( Mouse.MouseDownEvent, new MouseButtonEventHandler( CustomControlMouseDownEvent ) );
     _topMostParent = null;
  }
  </code></pre>
  
  <ul>
  <li>Use <code>Mouse.PreviewMouseDownOutsideCapturedElement</code> and add a handler to my control. In the handler I'd kick the control out of it's editing state. But I don't seem to get the event to fire. When does the Mouse.PreviewMouseDownOutsideCapturedElement get kicked off?</li>
  </ul>
  
  <p>Example code:</p>
  
  <pre><code>AddHandler( Mouse.PreviewMouseDownOutsideCapturedElementEvent, new MouseButtonEventHandler( EditableTextBlockPreviewMouseDownOutsideCapturedElementEvent ), true );
  </code></pre>