Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class SelectAllTextBoxBehavior
- {
- public static readonly DependencyProperty IsEnabledProperty =
- DependencyProperty.RegisterAttached(
- "IsEnabled",
- typeof(bool),
- typeof(SelectAllTextBoxBehavior),
- new UIPropertyMetadata(false, OnIsEnabledChanged));
- public static bool GetIsEnabled(DependencyObject dependencyObject)
- {
- return (bool)dependencyObject.GetValue(IsEnabledProperty);
- }
- public static void SetIsEnabled(DependencyObject dependencyObject, object value)
- {
- dependencyObject.SetValue(IsEnabledProperty, value);
- }
- private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- var textBox = d as TextBox;
- if (textBox == null) return;
- if ((bool) e.NewValue == true)
- {
- textBox.GotFocus += TextBoxOnGotFocus;
- }
- else
- {
- textBox.GotFocus -= TextBoxOnGotFocus;
- }
- }
- private static void TextBoxOnGotFocus(object sender, RoutedEventArgs routedEventArgs)
- {
- var senderBox = sender as TextBox;
- if (senderBox == null) return;
- // select all if text property contain text
- if (senderBox.Text.Length > 0)
- {
- senderBox.SelectAll();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement