Skip to content

Instantly share code, notes, and snippets.

@davidblurton
Created March 8, 2013 11:53
Show Gist options
  • Save davidblurton/5115943 to your computer and use it in GitHub Desktop.
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.
/// <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