Windows Phone Toolkit PhoneTextBox in depth
published on: 8/30/2011 | Views: N/A | Tags: WP7Toolkit Mango
by WindowsPhoneGeek
Recently a new version of the Windows Phone Toolkit was released: Windows Phone Toolkit - August 2011 (7.1 SDK) with some pretty interesting new components. Previously we covered all toolkit components in our 21 WP7 Toolkit in Depth articles covering all controls so it is time to continue this series with a few more posts.
In this post I am going to talk about the new "PhoneTextBox" control in details. Basically, PhoneTextBox is an advanced TextBox control with ActionIcon support, Hints and more. It also exposes a set of properties for rich customization.
Getting Started
To begin using PhoneTextBox first add a reference to the Microsoft.Phone.Controls.Toolkit.dll assembly in your Windows Phone application project.
UPDATE: If you have installed the toolkit via the .msi then you will not see the PhoneTextBox in the list with available controls. However this is a known issue and hopefully it will be fixed soon. For now here is the official discussion: http://silverlight.codeplex.com/workitem/9414 .The workaround is to download and rebuild the source code.
You can select Microsoft.Phone.Controls.Toolkit.dll directly from the "...\Silverlight for Windows Phone Toolkit Source & Sample - Aug 2011\Bin\" if you have downloaded the "Silverlight for Windows Phone Toolkit Source & Sample - Aug 2011.zip" instead.
You can create an instance of the PhoneTextBox control either in XAML or with C#.
- Define an instance of the PhoneTextBox control in XAML: you have to add the following namespace declaration:
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
<toolkit:PhoneTextBox Hint="UserName"/>
NOTE: Make sure that your page declaration includes the "toolkit" namespace! You can do this by using VisualStudio Toolbox, Expression Blend Designer or just include it on your own.
- ·Define an instance of the PhoneTextBox control in C#: just add the following using directive:
using Microsoft.Phone.Controls;
PhoneTextBox textBox = new PhoneTextBox(); textBox.Hint = "UserName";
Key Properties
PhoneTextBox derives from TextBox so it has all properties of the TextBox plus the following new ones:
- Hint
Hint is a dependency property of type string. It gets or sets the Hint of the PhoneTextBox control.
- HintStyle
HintStyle is a dependency property of type Style. It gets or sets the Hint Style of the PhoneTextBox control.
Example:
This example demonstrates how to add a simple HintStyle that changes only the FonFamily, FontSize and Foreground properties without changing the whole ControlTemplate.
<phone:PhoneApplicationPage.Resources>
<Style TargetType="ContentControl" x:Key="HintCustomStyle">
<Setter Property="FontFamily" Value="Calibri"/>
<Setter Property="Foreground" Value="Aqua"/>
<Setter Property="FontSize" Value="30"/>
</Style>
</phone:PhoneApplicationPage.Resources>
<!--...-->
<toolkit:PhoneTextBox Hint="Enter UserName" HintStyle="{StaticResource HintCustomStyle}"/>- ActualHintVisibility
ActualHintVisibility is a dependency property of type Visibility. It gets or sets whether the hint is actually visible.
- LengthIndicatorVisible
LengthIndicatorVisible is a dependency property of type Boolean. It determines whether the length indicator of the PhoneTextBox should be visible.
- LengthIndicatorTheshold
LengthIndicatorTheshold is a dependency property of type int. It determines the threshold after which the length indicator will appear.
Example:
<toolkit:PhoneTextBox Hint="Password" LengthIndicatorVisible="True" LengthIndicatorTheshold="10" />
- DisplayedMaxLength
DisplayedMaxLength is a dependency property of type int. It represents the displayed maximum length of text that can be entered. This value takes priority over the MaxLength property in the Length Indicator display.
Example:
<toolkit:PhoneTextBox Hint="Password" DisplayedMaxLength="4" LengthIndicatorVisible="True"/>
NOTE :In the given example you can enter more than 4 characters, but the length indicator displays the number of characters entered out of 4, even if the length of the text entered is greater than 4.
- ActionIcon
ActionIcon is a dependency property of type ImageSource. It gets or sets the ActionIcon image of the PhoneTextBox control.
Example1:
<toolkit:PhoneTextBox Hint="Search" ActionIcon="/Images/Search.png" ActionIconTapped="Search_ActionIconTapped"/>
private void Search_ActionIconTapped(object sender, EventArgs e)
{
//do some action here. I.e open MessageBox, etc.
MessageBox.Show("The action icon was tapped!");
}Example2:
<toolkit:PhoneTextBox Hint="Password" ActionIcon="wpglogo.png"/>
Key Events
- ActionIconTapped
Occurs when the ActionIcon is tapped.
Example:
phoneTextBox.ActionIconTapped += new EventHandler(phoneTextBox_ActionIconTapped);
//...
void phoneTextBox_ActionIconTapped(object sender, EventArgs e)
{
//...
}
That was all about the new "PhoneTextBox" from the Windows Phone Toolkit Aug 2011 in depth. Here is the full source code:
I hope that the post was helpful.
You can also follow us on Twitter @winphonegeek
Comments
posted by: RoguePlanetoid on 8/30/2011 2:52:15 PM
Great demo - this one can catch you out as in the DLL installed by the August Toolkit installer doesn't seem to have this as a component you can use seperately - I had to extract the code for the PhoneTextBox and add this as it's own component - unless this has changed, which I hope it has, as this is a very useful control!
posted by: Rich on 8/30/2011 7:18:49 PM
Thanks for the article. One question. How would you handle the ActionIconTapped event using MVVM? This control will be great, but I'm using the MVVM Light framework in my app and am trying to be consistent.
ActionIconTapped in MVVM Light
posted by: winphonegeek on 8/30/2011 7:31:11 PM
Basically, you have two options:
Option1: To use EventToCommand from MVVM Light
Option2: To use a custom behavior (that handles the event and calls a command)
@RoguePlanetoid
posted by: winphonegeek on 8/30/2011 7:52:34 PM
Thank you for pointing that out, we have updated the post.
The problem with the .msi is a known issue, hopefully it will be fixed soon: http://silverlight.codeplex.com/workitem/9414
Good Control
posted by: Carlos Peres on 8/30/2011 11:16:09 PM
Amazing post guys. Pretty good example as always. I was about to miss this control. I installed the toolkit several days ago but did not notice any PhoneTextBox. Now I downloaded the source and it is in there :) I was looking for such component for a long time. I mean there are similar payed controls but the Toolkit`s PhoneTextBox is the only FREE one.
RE:ActionIconTapped in MVVM Light
posted by: winphonegeek on 8/31/2011 3:28:38 PM
We posted an new article that explains: How to bind a Windows Phone control Event to a Command using MVVM Light
The article uses PhoneTextBox and ActionIconTapped as an example.
Password box?
posted by: Aaron on 1/23/2012 12:01:39 AM
Great article, thanks!
I was wondering if anyone has come up with a way to use this for a password box, i.e. when the characters are masked.
Hint at the center
posted by: Aditya on 2/10/2012 11:38:02 AM
How to align hint at the center of phonetextbox? I have set horizontal alignment to center but its not working.
Thanks
posted by: Thanks on 4/13/2012 11:35:08 AM
Thanks
posted by: waz on 5/11/2012 10:09:28 AM
how to change the border color when focus
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
