Text Controls

WPF provides controls for editing and displaying text. The simplest text editing control is TextBox. By default, it allows a single line of text to be edited, but by setting AcceptsReturn to true, it can edit multiple lines. It provides basic text editing facilities: selection support, system clipboard integration (cut, paste, etc.), and multilevel undo support.

Example 5-8 shows two TextBox elements, one with default settings and one in multiline mode.Figure 5-9 shows the results. (To illustrate the multiline text box, I typed "Enter" in the middle of the text before taking the screenshot.) Example 5-8 and Figure 5-9 also show PasswordBox, which is similar to TextBox, but is designed for entering passwords. As you can see, the text in the PasswordBox has been displayed as a line of identical symbols. This is common practice to prevent passwords from being visible to anyone who can see the screen. You can set the symbol with the PasswordChar property. The PasswordBox also opts out of the ability to copy its contents to the clipboard.

Example 5-8. TextBox and PasswordBox

<StackPanel Orientation="Horizontal">

  <TextBox Margin="5" VerticalAlignment="Center" Text="Single line textbox" />

  <TextBox AcceptsReturn="True" Margin="5" Height="50"
           VerticalScrollBarVisibility="Visible"
           VerticalAlignment="Center" Text="Multiline textbox" />

  <PasswordBox Margin="5" VerticalAlignment="Center" Password="Un5ecure" />

</StackPanel>

Figure 5-9. TextBox and PasswordBox

TextBox and PasswordBox ...

Get Programming WPF, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.