WPF Lost-Focus issue in same control

2019-08-09 13:50发布

问题:

I have a small problem with focusing in .net/wpf. I have written a custom user control which contains a TextBox and a SearchButton. The user-control has a lost focus event, which validate the content of the textbox if the focus is leaving. But now i have the problem, if i click on my search button, the lost focus event of the user control gets fired, even if i click on the button in the custom user control. (The button additionally has the option TabStop="False":

The problem is, that i don't want to fire the event, if i click on the button.

Does any one has an idea?

Thank you!

回答1:

Set

Focusable="False"

of your search button and the TextBox will not lose the focus because the button doesn't get the focus.



回答2:

You can Do this Check in Event like this

protected void LostFocusEvent(object sender, RoutedEventArgs e)
{
    if(textBox.Text.Length>0)
    {
      // if there is any character in TextBox
    return;
    }

   // your validation Code goes here
}