Windows Phone 7 Silverlight: Behaviors for TextBox
- modified:
- reading: 5 minutes
I’ve submitted my first application for Windows Phone! It is very simple app. Marketplace has a good amount of similar apps: Planning Poker. Anyway, I’ve wrote couple own behaviors for TextBox and want to share this code with you.
If you are Silverlight/WPF developer and have already used Blend than you should know what are Behaviors and Interactions. If not or if you forget what it is I will remind you. You can find these libraries in directory “c:\Program Files (x86)\Microsoft SDKs\Expression\Blend\” (in case if you have Windows x86 than you should remove from path (x86)). And of course these libraries exist only in case if Blend has been installed. In this directory you can find packs of libraries for WPF/Silverlight and WindowsPhone. If you don’t know what are Behaviors and Interaction I suggest you to read Expression Blend SDK for Windows Phone on MSDN. In few words: this is approach to extend default control’s features, more than! this approach allows you to use MVVM pattern.
Let’s extend some features of default TextBox control.
FocusOnLoadedBehavior
If you will try to open phone contact’s item to edit on your device you’ll see that device will set focus on first TextBox. This is very good user experience: if you open something for edit than most likely you will put focus on TextBox to edit data in this TextBox. Otherwise it is good question what if this page will have more than one TextBox, should we place focus on first of them or not? Windows Phone does it, it places focus on first TextBox if you will open edit page of contact’s name.
I found that it will be good to do the same in some of my pages, so I wrote next behavior:
```csharp ///It is easy to use it. Just place it for one of your TextBox like I will show below and this behavior will set focus on Loaded event (in most cases it will be the moment when user opens page with this control):
```csharpI’ve put namespace definitions “interactivity” and “oBehavior” to TextBox attributes just for example, you can move it on top level, for example you can add it to your page attributes.
This behavior has property SetFocusOnLoad. If you will need to manage when you need set this focus and when not – you can use this property. For example it can be the case when you have two TextBoxes and you need to decide which of them should get focus on page load.
TextBoxSelectOnFocusBehavior
This is second must have behavior. If user (or you) set focus to TextBox than in different cases user can expect different default position of cursor. For example if it is TextBox for edit some word than most likely user don’t want to add some letters to this word, in most cases he will delete this word to write new one (for example it can be dictionary), so it is good UX to select whole word on focus set event. To do so you can use next behavior:
```csharp public enum TextBoxFocusSelectType { None = 0, SelectAll = 1, SetCursorToTheEnd = 2 } ///To use it just add it to TextBox:
```xmlTextBoxInputRegexFilterBehavior
The last behavior for today TextBoxInputRegexFilterBehavior, which allows you to filter input text. Actually this behavior can add some unobviousness behavior to your application: user sees some button, but when he presses it he doesn’t see any changes. So you should use it very careful. One place where I used it: I need to get integer values from user, but TextBox has minimum InputScope=”Number” which has digits and dot (or comma, it depends on your device language). I saw in one of the apps that one developer created his own keyboard to get rid of this problem. I didn’t like it, and of course I don’t want to use validators for Windows Phone applications, we have very small screen for validators. So I’ve wrote next behavior:
```csharp ///To use it:
```xmlIn next topics I will show you some other behaviors which I use for my Windows Phone applications. And don’t forget to read section Expression Blend SDK for Windows Phone on MSDN!