我有他們的標題上帶有TextBox的tabItems。我使用LostFocus和MouseDoubleClick事件將文本設置爲TextBox。WPF TabItem失去焦點事件
<TabControl>
<TabItem Width="50">
<TabItem.Header>
<TextBox Text="text" IsReadOnly="True" LostFocus="TextBox_LostFocus" MouseDoubleClick="TextBox_MouseDoubleClick"/>
</TabItem.Header>
</TabItem>
</TabControl>
private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
TextBox text_box = sender as TextBox;
if (text_box == null) { return; }
text_box.IsReadOnly = false;
text_box.SelectAll();
}
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
TextBox text_box = sender as TextBox;
if (text_box == null) { return; }
text_box.IsReadOnly = true;
}
如果只點擊TextBox外部的TabItem標題區域或enother TabItem,就會發生LostFocus事件。 單擊標籤項目內容區域不會觸發丟失的焦點事件。
如何在用戶單擊TextBox外部的任何區域時使TextBox失去焦點?