When using the TextBox's Previewmouseleftbuttonup event (for example, when the mouse clicks into the textbox, clearing the current input), it is very surprising to find that no matter how many clicks can not click on the other controls, the focus has been occupied by the text box.
The solutions and test cases are as follows:
Interface
1 <Windowx:class= "LEARNWPF." MainWindow "2 xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"3 xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"4 Title= "MainWindow"Height= " the"Width= "525">5 <Grid>6 <ListBox>7 <TextBoxx:name= "TextBox"Previewmouseleftbuttonup= "Textbox_previewmouseleftbuttonup"Text= "Test text"Width= "Max"Height= "+"/>8 <Buttonx:name= "BTN"Click= "Btn_click"Width= "+"Height= "+" />9 </ListBox>Ten </Grid> One </Window>
Logic
1 usingSystem.Windows;2 usingSystem.Windows.Controls;3 usingSystem.Windows.Input;4 5 namespaceLEARNWPF6 {7 /// <summary>8 ///Interaction logic for MainWindow.xaml9 /// </summary>Ten Public Partial classMainwindow:window One { A PublicMainWindow () - { - InitializeComponent (); the } - - Private voidBtn_click (Objectsender, RoutedEventArgs e) - { +MessageBox.Show ("Button ' s clicked"); - } + A Private voidTextbox_previewmouseleftbuttonup (Objectsender, MouseButtonEventArgs e) at { -Textbox. Text =string. Empty; -e.handled =true; - //to remove a textbox from capturing a mouse - (textbox). Releasemousecapture (); - } in } -}
Previewmouseleftbuttonup events for textbox in WPF