/ Published in: C#
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
public class FocusSelectTextBox : TextBox { public FocusSelectTextBox() { AddHandler(PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(SelectivelyIgnoreMouseButton), true); } private static void SelectivelyIgnoreMouseButton(object sender, MouseButtonEventArgs e) { // Find the TextBox DependencyObject parent = e.OriginalSource as UIElement; parent = VisualTreeHelper.GetParent(parent); if (parent == null) return; var textBox = (TextBox)parent; if (textBox.IsKeyboardFocusWithin) return; // 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(); } }