Last active
August 29, 2015 14:15
-
-
Save jrgcubano/e8e9c79be44f0780c1ef to your computer and use it in GitHub Desktop.
WPF ClickSelectableTextBox for tablets
This file contains hidden or 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
// | |
// Deriving from TextBox | |
// | |
public class ClickSelectTextBox : TextBox | |
{ | |
public ClickSelectTextBox() | |
{ | |
AddHandler(PreviewMouseLeftButtonDownEvent, | |
new MouseButtonEventHandler(SelectivelyIgnoreMouseButton), true); | |
AddHandler(GotKeyboardFocusEvent, | |
new RoutedEventHandler(SelectAllText), true); | |
AddHandler(MouseDoubleClickEvent, | |
new RoutedEventHandler(SelectAllText), true); | |
} | |
private static void SelectivelyIgnoreMouseButton(object sender, | |
MouseButtonEventArgs e) | |
{ | |
// Find the TextBox | |
DependencyObject parent = e.OriginalSource as UIElement; | |
while (parent != null && !(parent is TextBox)) | |
parent = VisualTreeHelper.GetParent(parent); | |
if (parent != null) | |
{ | |
var textBox = (TextBox)parent; | |
if (!textBox.IsKeyboardFocusWithin) | |
{ | |
// If the text box is not yet focussed, give it the focus and | |
// stop further processing of this click event. | |
textBox.Focus(); | |
e.Handled = true; | |
} | |
} | |
} | |
private static void SelectAllText(object sender, RoutedEventArgs e) | |
{ | |
var textBox = e.OriginalSource as TextBox; | |
if (textBox != null) | |
textBox.SelectAll(); | |
} | |
} | |
// | |
// General solution in app for all textboxes | |
// | |
public partial class App : Application | |
{ | |
protected override void OnStartup(StartupEventArgs e) | |
{ | |
// Select the text in a TextBox when it receives focus. | |
EventManager.RegisterClassHandler(typeof(TextBox), TextBox.PreviewMouseLeftButtonDownEvent, | |
new MouseButtonEventHandler(SelectivelyIgnoreMouseButton)); | |
EventManager.RegisterClassHandler(typeof(TextBox), TextBox.GotKeyboardFocusEvent, | |
new RoutedEventHandler(SelectAllText)); | |
EventManager.RegisterClassHandler(typeof(TextBox), TextBox.MouseDoubleClickEvent, | |
new RoutedEventHandler(SelectAllText)); | |
base.OnStartup(e); | |
} | |
void SelectivelyIgnoreMouseButton(object sender, MouseButtonEventArgs e) | |
{ | |
// Find the TextBox | |
DependencyObject parent = e.OriginalSource as UIElement; | |
while (parent != null && !(parent is TextBox)) | |
parent = VisualTreeHelper.GetParent(parent); | |
if (parent != null) | |
{ | |
var textBox = (TextBox)parent; | |
if (!textBox.IsKeyboardFocusWithin) | |
{ | |
// If the text box is not yet focused, give it the focus and | |
// stop further processing of this click event. | |
textBox.Focus(); | |
e.Handled = true; | |
} | |
} | |
} | |
void SelectAllText(object sender, RoutedEventArgs e) | |
{ | |
var textBox = e.OriginalSource as TextBox; | |
if (textBox != null) | |
textBox.SelectAll(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment