Content Model: A TextBox can contain only unformatted text. See TextBox Content Model Overview for more information.
The following graphic shows an example of a TextBox.
Example of a TextBox
.gif)
TextBox is a composite control that that is composed of several encapsulated components. Consequently, some events do not bubble up to the containing control because they are handled by encapsulated child elements. Because of this, application developers should listen for the tunneling version of an event (denoted by the prefix "Preview").
TextBox supports unformatted text only. For applications that require support for richer content, see RichTextBox. For applications that need to accept passwords or other sensitive input, see PasswordBox.
Horizontally and vertically aligning text within a TextBox is done with the HorizontalContentAlignment and VerticalContentAlignment properties. Aligning the TextBox within the layout of the page is done with the HorizontalAlignment and VerticalAlignment properties.
The best way to hide the border around a TextBox is to set the BorderThickness property of the TextBox to 0.
Important Note: |
|---|
TextBox has built-in handling for the bubbling MouseUp and MouseDown events. Consequently, custom event handlers that listen for MouseUp or MouseDown events from a TextBox will not be called. If you need to respond to these events, listen for the tunneling PreviewMouseUp and PreviewMouseDown events instead, or register the handlers with the HandledEventsToo argument (this latter option is only available through code). Do not mark the event handled unless you deliberately want to disable TextBox native handling of these events, and be aware that this has notable effects on the control's UI.
|
Scrollbars are not visible on a TextBox by default. To make scrollbars visible, set the VerticalScrollBarVisibility and HorizontalScrollBarVisibility properties to Visible or Auto.
Usually the TextChanged event should be used to detect whenever the text in a TextBox or RichTextBox changes rather then KeyDown as you might expect. See How to: Detect When Text in a TextBox Has Changed for an example.