WPF Lost-Focus issue in same control

喜夏-厌秋 提交于 2019-12-24 15:32:56

问题


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
}


来源:https://stackoverflow.com/questions/31492226/wpf-lost-focus-issue-in-same-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!