I want every TextBox in my application to select all its text when the user focuses on it. To do this, I put the following in my App.xaml file:
<Application.Resources>
<Style TargetType开发者_运维问答="TextBox" x:Key="tbkey">
<EventSetter Event="GotFocus" Handler="textBoxFocus"/>
</Style>
</Application.Resources>
and the following code in the App.xaml.cs file:
private void textBoxFocus(object sender, RoutedEventArgs a)
{
TextBox t = sender as TextBox;
t.SelectAll();
}
However, the method is never called when a TextBox is focused in my application. I think it is because I am not putting the handler method in the right location but I have no idea where that would be. Any ideas?
Remove x:Key="tbkey" and your textBoxFocus method will be fired.
EDIT
Source Code can be downloaded here
精彩评论