Wednesday, January 27, 2010

Silverlight 3: Auto select text in TextBox

Missing out of the box in Silverlight 3 is the ability to auto select the text in a textbox when it receives focus. Thankfully Attached Properties make it very easy to add the functionality. In this post I will create a AutoSelectText attached property, which when set to true will select all the text as when the textbox receives focus. And because of routed events, it is not required to add the property to every textbox on a page. Instead it can be added to the parent content control and all child textboxes will have to auto select behavior.

Usage

  1. <Grid x:Name="LayoutRoot"
  2.     attachedProperties:AttachedProperties.AutoSelectText="true">
  3.     <StackPanel Orientation="Vertical"
  4.                 HorizontalAlignment="Left">
  5.                  <TextBox Text="Will not auto select"
  6.                  MaxWidth="300"
  7.                  Margin="4"
  8.                  attachedProperties:AttachedProperties.PreventAutoSelectText="true" />
  9.         <TextBox Text="Will auto select 1"
  10.                  MaxWidth="300"
  11.                  Margin="4"/>
  12.         <TextBox Text="Will auto select 2"
  13.                  MaxWidth="300"
  14.                  Margin="4" />
  15.     </StackPanel>
  16. </Grid>

Line 2: The AutoSelectText is set to true on the root level content control (Grid). That’s it. All text boxes within the Grid will participate in the auto select text functionality.
Line 8: A control can opt out by setting the PreventAutoSelectText property to false.

Listed below are some of the key code blocks. The link to the entire source code can be found at the end of the post.

Declaring the attached property

  1. public static readonly DependencyProperty AutoSelectTextProperty = DependencyProperty.RegisterAttached("AutoSelectText",
  2.                                                                        typeof(Boolean),
  3.                                                                        typeof(AttachedProperties),
  4.                                                                        new PropertyMetadata(OnAutoSelectTextChanged));
  5.  
  6. public static readonly DependencyProperty PreventAutoSelectTextProperty = DependencyProperty.RegisterAttached("PreventAutoSelectText",
  7.                                                                               typeof(Boolean),
  8.                                                                               typeof(AttachedProperties),
  9.                                                                               null);

Line 1-2: A new boolean attached property called AutoSelectText is declared.
Line 4: The OnAutoSelectTextChanged event handler is registered, which is be invoked when the value of the property is set in the xaml.
Line 6: A new boolean attached property called PreventAutoSelectText is declared. Controls can use this property to opt out of the auto select functionality.

The dependency property changed event handler

  1. private static void OnAutoSelectTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  2. {
  3.     //This works because of event bubbling.
  4.     FrameworkElement frameworkElement = d as FrameworkElement;
  5.     if (frameworkElement != null)
  6.     {
  7.         if ((bool)e.NewValue)
  8.             frameworkElement.GotFocus += OnGotFocus;
  9.         else
  10.             frameworkElement.GotFocus -= OnGotFocus;
  11.     }
  12. }

Line 8: If the value of the AutoSelectText (accessed using e.NewValue) is true, we add the OnGotFocus event handler to the GotFocus event of the control on which the AutoSelectText property is being set (from the xaml).
Line 9: Similarly if the control does not wish to participate in auto selecting of text, we remove our event handler.

The GotFocus event handler

  1. private static void OnGotFocus(object sender, RoutedEventArgs e)
  2. {
  3.     TextBox textBox = FocusManager.GetFocusedElement() as TextBox;
  4.     if (textBox != null && !(bool)textBox.GetValue(PreventAutoSelectTextProperty))
  5.         textBox.Select(0, textBox.Text.Length);
  6. }

Line 3: Since we are using routed events, the sender parameter will not be the textbox that currently has focus. It will the root level content control (Grid) which has the AutoSelectText attached property. The FocusManager class is used to get a reference to that control that has the focus.

Why two attached properties?

Silverlight does not provide a way to check if a control has a attached property defined in the xaml. Due to this if I replace

  1. if (textBox != null && !(bool)textBox.GetValue(PreventAutoSelectTextProperty))

in the OnGotFocus method with

  1. if (textBox != null && !(bool)textBox.GetValue(AutoSelectTextProperty))

I will always get false for the textBox.GetValue call, even if the AutoSelectText has not been defined for the textbox in xaml.

Download source code for AutoSelectText attached property.

Subscribe to my feed in your favorite feed reader