title: Using Mouse.Capture to detect a click outside of a given control
body: <p>I have a TextBox in a WPF project where I am trying to detect a mouse click anywhere on the Application other than in the TextBox. </p>
  
  <p>Here is the code that I have so far.</p>
  
  <pre><code>System.Windows.Input.MouseButtonEventHandler clickOutsideHandler;
  
  public MyClass() {
      clickOutsideHandler = new System.Windows.Input.MouseButtonEventHandler(HandleClickOutsideOfControl);
  }
  
  private void StartCapture() {
      System.Windows.Input.Mouse.Capture(TextBox1, System.Windows.Input.CaptureMode.SubTree);
      AddHandler(System.Windows.Input.Mouse.PreviewMouseDownOutsideCapturedElementEvent, clickOutsideHandler, true);
  }
  
  private void HandleClickOutsideOfControl(object sender, System.Windows.Input.MouseButtonEventArgs e) {
      ReleaseMouseCapture();
      RemoveHandler(System.Windows.Input.Mouse.PreviewMouseDownOutsideCapturedElementEvent, clickOutsideHandler);
  }
  </code></pre>
  
  <p>The problem I'm having is that the event handler never gets called. I've tried capturing the return value of the Capture() function, but it shows as true. Can anyone show me what I'm doing wrong?</p>