GotFocus和PreviewLeftButtonDown事件
生活随笔
收集整理的這篇文章主要介紹了
GotFocus和PreviewLeftButtonDown事件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
當(dāng)TextBox獲得焦點(diǎn)后,其中的文字會(huì)被全選。通過GotFocus和PreviewLeftButtonDown事件,就可以模擬上述行為。
如果用戶只是用鍵盤操作,GotFocus事件就足夠了。
如果使用鼠標(biāo)操作,就要用到2個(gè)事件了。TextBox會(huì)將光標(biāo)放在鼠標(biāo)單擊的那個(gè)位置。為了防止這種情況,會(huì)實(shí)現(xiàn)PreviewLeftButtonDown事件,
在該控件獲得焦點(diǎn)后,將該事件的Handled屬性設(shè)置為true。這會(huì)終止該事件,從而防止光標(biāo)被定位到TextBox中的特定位置。
private void timeAllowedTextBox_GotFocus(object sender, RoutedEventArgs e){timeAllowedTextBox.SelectAll();}private void timeAllowedTextBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e){var control = sender as TextBox;if (control == null)return;Keyboard.Focus(control);e.Handled = true;}?
轉(zhuǎn)載于:https://www.cnblogs.com/tirmer/p/4075116.html
總結(jié)
以上是生活随笔為你收集整理的GotFocus和PreviewLeftButtonDown事件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大鹏新片《热烈》今日杀青年内上映 黄渤化
- 下一篇: multiselect多选下拉框