Skip to content

Instantly share code, notes, and snippets.

@smailliwcs

smailliwcs/App.cs

Created Sep 22, 2020
Embed
What would you like to do?
Selecting text on keyboard navigation in WPF
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
RegisterClassHandlers();
Window window/* = ... */;
window.Show();
}
private void RegisterClassHandlers()
{
EventManager.RegisterClassHandler(
typeof(TextBox),
UIElement.GotKeyboardFocusEvent,
new KeyboardFocusChangedEventHandler(TextBox_GotKeyboardFocus));
EventManager.RegisterClassHandler(
typeof(TextBox),
UIElement.LostFocusEvent,
new RoutedEventHandler(TextBox_LostFocus));
}
private void TextBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (e.KeyboardDevice.IsKeyDown(Key.Tab))
{
((TextBox)sender).SelectAll();
}
}
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
((TextBox)sender).Select(0, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.