同一控件中的 WPF 失去焦点问题

WPF Lost-Focus issue in same control

我在聚焦 .net/wpf 时遇到了一个小问题。我编写了一个自定义用户控件,其中包含一个 TextBox 和一个 SearchButton。用户控件有一个失去焦点事件,如果焦点离开,它会验证文本框的内容。但是现在我遇到了问题,如果我单击我的搜索按钮,用户控件的失去焦点事件将被触发,即使我单击自定义用户控件中的按钮也是如此。 (该按钮还有选项 TabStop="False":

问题是,我不想在单击按钮时触发事件。

您可以像这样进行签到活动

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
}

设置

Focusable="False"

您的搜索按钮和 TextBox 不会失去焦点,因为按钮没有获得焦点。