2

I new to WPF C# and I'm looking for some direction. I have a text box that pops up on an error, I want the user to be able to click any where on the form to close this window. I'm not really sure of what search terms to use or where to start. Any help or smart ass comments like, "have you heard of google" are appreciated :)

like, if mouse clicked hide();

Thanks, rd42

My final solution:

private void Button_Click(object sender, RoutedEventArgs e)
{
    this.Hide();
}

4 Answers 4

5

Here is an easy solution using a Popup window.

<Popup Name="errMsg" StaysOpen="False">
  <TextBox/>
</Popup>

From your codebehind, just do this when it is time to display the popup.

errMsg.IsOpen = true;

Clicking anywhere outside of the Popup will produce the desired effect. If you are using a traditional window, just subscribe to its LostFocus or Deactivate event and close it there (assuming that it is not shown as a dialog). Honestly though, the Popup is the easiest and best solution for cases like this.

1
  • +1. Was about the post something similar. Much prettier than manually handling MouseUp events as well.
    – Bubblewrap
    May 2, 2011 at 12:29
2

Subscribe to the MouseUp event of the Window instance.

0
1

MouseUp event in the form should be the one you are looking for

1

Have found on another forum:

Use Window.Deactivate event & inside the event implementation, close the window... that's it... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.