Created
March 8, 2013 11:53
-
-
Save davidblurton/5115943 to your computer and use it in GitHub Desktop.
You can override the default event handlers on certain controls in App.xaml.cs. For example, we put the cursor at the end of the text on textbox gotfocus (the default is at the start) and we select the text in a password box.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Interaction logic for App.xaml | |
/// </summary> | |
public partial class App : Application | |
{ | |
... | |
protected override void OnStartup(StartupEventArgs e) | |
{ | |
EventManager.RegisterClassHandler(typeof(TextBox), UIElement.GotFocusEvent, new RoutedEventHandler(TextBox_GotFocus)); | |
EventManager.RegisterClassHandler(typeof(PasswordBox), UIElement.GotFocusEvent, new RoutedEventHandler(PasswordBox_GotFocus)); | |
base.OnStartup(e); | |
} | |
private void TextBox_GotFocus(object sender, RoutedEventArgs e) | |
{ | |
TextBox textbox = sender as TextBox; | |
if (textbox != null) | |
{ | |
textbox.CaretIndex = textbox.Text.Length; | |
} | |
} | |
private void PasswordBox_GotFocus(object sender, RoutedEventArgs e) | |
{ | |
PasswordBox passwordBox = sender as PasswordBox; | |
if (passwordBox != null) | |
{ | |
passwordBox.SelectAll(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment