Silverlight / Windows Phone 7 Behavior to enhance Usability of TextBoxes

Yesterday I used an application on my Windows Phone which was very useful. When you gave focus to a TextBox it would immediately select all of the content of the TextBox. Though it’s not difficult to implement I found it very useful, and see it as a productivity enhancement for the users of the application.

The behavior is applied on any TextBox, and will attach to the GotFocus event and on the occurrence of the event it will execute the SelectAll method of the TextBox.

public class SelectAllOnFocusBehavior : Behavior<TextBox>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.GotFocus += GotFocus;
    }

    private void GotFocus(object sender, RoutedEventArgs e)
    {
        AssociatedObject.SelectAll();
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.GotFocus -= GotFocus;
    }
}

 

And using it is really simple. Just like any other Behavior add it to the Interaction.Behaviors collection of the TextBox where you want to apply this behavior.

<TextBox Text="Some content">
    <I:Interaction.Behaviors>
        <Behaviors:SelectAllOnFocusBehavior />
    </I:Interaction.Behaviors>
</TextBox>

 

The result could like like this, after giving focus to the TextBox.

Behavior applied to the TextBox in a Windows Phone application

Microsoft DevDays 2011 - I’m attending

devdays_loungeYes I’m attending the DevDays this year, but only one day. I will be representing Sixin in the Community Lounge on Thursday April 28th. The last day you can find Antoni Dol representing Sixin at the same place.

If my schedule allows it, I will try to attend a couple of sessions as well. I’m thinking about the following agenda.

9:15 – 10:45 Keynote by Rob Miles, Wade Wegner, Scott Hanselman and Ben Riga.

11:05 – 12:20 Windows Phone 7, title will be announced after Mix11 by Ben Riga.

13:30 – 14:45 Reactive Extensions for .NET for the Rest of Us by Mike Taulty. I’m really doubting how this session could be a level 300 session, I would expect it to be level 400. Rx had been mind blowing for me so far.

15:05 – 16:20 Science of Great UI by Mark Miller. Though I’m still a developer, not great at UI, but I want to improve my skills.

But of course see me at the for some talks around Silverlight, for example Silverlight 5, WP7, or anything else.