Silverlight for Windows Phone provides several controls for rendering text, along with a set of properties for formatting the text. The text-based controls available in Silverlight are TextBlock, TextBox, and PasswordBox. This QuickStart shows you how you can use these text controls to display and enter text.
This QuickStart contains the following sections:
The TextBlock is the primary control for displaying read-only text in Windows Phone applications. You can display text in a TextBlock control by using its Text property.
The following XAML shows how to define a TextBlock control and set its Text property to a string.
The following illustration displays the result of the previous XAML.
You can also display a series of strings in a TextBlock, where each string has a different formatting. You can do this by using a Run element to display each string with its formatting and by separating each Run element with a LineBreak element.
The following XAML shows how to define several differently formatted text strings in a TextBlock by using Run objects separated with a LineBreak.
The following illustration shows the result of the previous XAML.
For more information about TextBlock, see Text and Fonts and TextBlock.
You can use a TextBox control to enter and edit single-format and multi-line text. You can use the TextBox.Text property to set the text in a TextBox. In the following live sample, there are three text boxes. When you enter text in the first TextBox, the same text is displayed in the second TextBox. This is implemented by using the TextChanged event. The third TextBox displays a watermark text. To implement the watermark text, you can use various font properties, such as Foreground and FontSize, and events, such as GotFocus and LostFocus.
To try this live sample, click and type in the first and third text boxes.
Note: The live samples in this QuickStart use Silverlight running in the browser to simulate the behavior in Silverlight for Windows Phone. The actual behavior may be slightly different in the Windows Phone emulator or on a Windows Phone device.
The main way by which you enter text in a Windows Phone application is by using a small onscreen keyboard, which is called a software input panel or SIP. The SIP keyboard opens by sliding up automatically from the bottom of the screen when an editable control such as a TextBox becomes active. When a user taps outside of the edit control, scrolls a list, or presses the Back Button, it closes by sliding down off the bottom of the screen. If a phone has a hardware keyboard (which is a phone manufacturer option) and it is deployed, the SIP keyboard will automatically close. You can specify the keyboard layout for the SIP, which provides you with easy access to the expected input characters based on the application context. For example, when you use the SIP to enter a postal code, you only want to see numeric keys. You can specify the keyboard layout for a TextBox by using the InputScope property.
The following table lists some of the commonly used SIP layout and the associated InputScopeNameValue that you specify in XAML or code.
| Input Scope | Description | Illustration |
|---|---|---|
| Default | This is the default layout when no input scope is specified. This displays the standard QWERTY layout and auto-capitalizes the first letter of a sentence. | |
| Text | Use this layout when the user will mostly type standard text. This layout provides text suggestions, auto-correction, auto-apostrophe, auto-accent, and auto-capitalization. This layout also has access to letters, numbers, symbols, and ASCII-based emoticons. You can use this layout in fields such as email and documents. | |
| Chat | Use this layout when the user uses this to type standard words, slang, and abbreviations. This layout provides text suggestions while typing and tapping on a word, auto-correction, auto-apostrophe, auto-accent, and auto-capitalizes the first letter of a sentence. This layout also has access to letters, numbers, symbols, and rich emoticons. You can use this layout in fields such as SMS, IM, and Twitter clients.Use this layout when the user will mostly type standard words, slang, and abbreviations. This layout provides text suggestions, auto-correction, auto-apostrophe, auto-accent, and auto-capitalization. This layout also has access to letters, numbers, symbols and rich emoticons. You can use this layout in fields such as SMS, IM, and Twitter clients. | |
| URL | Use this layout when the user must type an URL. This layout turns off all auto correct features and provides a web layout with .com and Go keys. | |
| TelephoneNumber | Use this 12-key layout for phone numbers. | |
| EmailSmtpAddress | Use this layout for email addresses. This layout includes @ and .com keys, and easy access to phone number layout. | |
| CurrencyAmount | Use this layout for currency input. | |
There are more than 60 different input scopes. For a complete list of InputScope values supported in Windows Phone, see the InputScopeNameValue topic.
The following example shows how you can set the input scope for a TextBox control by using XAML and C#.
You can use the PasswordBox control to enter passwords. The user can't see the entered text; only password characters that represent the text are displayed. You can use the Password property to get or set the password and use the PasswordChar property to specify the password character.
For more information on PasswordBox control see, PasswordBox topic.Now that you're familiar with the basic UI components of controls and text, it's time to position them where you want in your application: Layout QuickStart.
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/