RichTextBox Control
The Windows Phone SDK includes a specialized control for displaying formatted text. This control is called the RichTextBox
. With the RichTextBox
control you can format text by using a simplified markup including paragraph, bold, italic, and hyperlink tags.
The format is meant to provide some level of formatting like HTML
text allows without requiring the complexity (or power) of the full HTML
stack. For example:
<RichTextBox>
<Paragraph>You can use inline tags to format
<Bold>bold</Bold> text and even add
<Italic>italics</Italic>.
</Paragraph>
<Paragraph>Using this Markup you can add
<LineBreak /> line breaks and even add
<Hyperlink NavigateUri="/SomePage.xaml">hyperlinks</Hyperlink>!
</Paragraph>
<Paragraph>Also arbitrary XAML:
<InlineUIContainer>
<StackPanel>
<Ellipse Fill="Red"
Width="25"
Height="25" />
<TextBox />
</StackPanel>
</InlineUIContainer>
</Paragraph>
</RichTextBox>
The RichTextBox
control supports a number of tags, as shown in Table 2.
Table 2. RichTextBox
Markup Tags
Content Controls
Content controls specifically allow you to contain arbitrary XAML inside them. The most common of these is the Button
control. For example, to simply show a text message in a button you could just set the Content
property, like so:
<StackPanel>
<Button Content="Click Me" />
</StackPanel>
You can see that the content of the control (“Click Me”) is now inside the button, as shown in Figure 6.
Figure 6. Simple button with simple content
But the content can take arbitrary XAML content as well:
<StackPanel>
<Button>
<Button.Content>
<StackPanel>
<Image Source="headshot.png"
Width="100"/>
<TextBlock>Hello</TextBlock>
</StackPanel>
</Button.Content>
</Button>
</StackPanel>
This results in a button containing XAML, as shown in Figure 7.
Figure 7. Button with XAML content
Notice that the content is inside the button, not replacing the XAML that makes up the button. Setting the Content
property allows you to specify what is inside the button (or in most
content controls, what is inside some part of the control). A content
control is any control that derives from the ContentControl
class. These include Button
, CheckBox
, RadioButton
, and HyperlinkButton
.
List Controls
List controls support showing any arbitrary list of items. They do this by using a property called ItemsSource
. This property takes any collection that supports IEnumerable
or IList
. This means any type of collection (from simple arrays to complex generic collections) is supported by the list controls. The ListBox
defined in XAML is pretty standard:
<StackPanel>
<ListBox x:Name="theList" />
</StackPanel>
The real trick is when you set some collection to the ItemsSource
property:
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
theList.ItemsSource = new string[] { "One", "Two", "Three" };
}
}
Setting the ItemsSource
will show the collection and allow individual items to be selected, as shown in Figure 8.
Figure 8. List box
Other list controls will follow this same interface (of setting the collection to an ItemsSource
) to set the collection. By using these simple control sets, you should be able to create great experiences for your users.