The WPF TextBox has the ability to accept multiline input. The blog post "Enable WPF TextBox Multiline Text" provides code snippets to enable a WPF TextBox for multiline text and to read text from the TextBox.
![]() |
Enable WPF Textbox Multiline Text |
Getting Started
There are two textbox attributes which makes the WPF textbox to to accommodate multiple lines of text. Those are TextWrapping
and AcceptsReturn
. The TextWrapping
attribute allows warp to intered text to a new line when the edge of the textbox control is reached by expanding the height of the textbox control to include room for a new line, if necessary. The AcceptReturn attribute inserts a new line at the current cursor position
To enable a WPF TextBox for multiple lines, you need to set the TextWrapping
property to Wrap and the AcceptReturn
property to True. To allow the content of the TextBox to scroll, set the VerticalScrollBarVisibility
property to Visible.
Enable WPF Textbox Multiline Text
The following code example shows how to use Extensible Application Markup Language (XAML) to define a TextBox
control that automatically expands to accommodate multiple lines of text.
<TextBox
Name="txtmulitiline" Grid.Row="1"
TextWrapping="Wrap"
AcceptsReturn="True"
BorderBrush="Black"
BorderThickness="1"
VerticalScrollBarVisibility="Visible"
HorizontalScrollBarVisibility="Disabled"
FontWeight="Bold">
</TextBox>
C# Code
this.txtmulitiline.TextWrapping = TextWrapping.Wrap;
this.txtmulitiline.AcceptsReturn = true;
this.txtmulitiline.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
this.txtmulitiline.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
Adding Text to WPF Multiline TextBox
To add text to a multiline TextBox
in WPF, you can do it programmatically from your C# code-behind by using its Text
property.
txtmulitiline.Text = "This is a new message in multiline textbox.";
This replaces any existing content
Append Texttxtmulitiline.Text += "New line of text added." + Environment.NewLine;
Above code will keep existing content and add more,use he += operator and add a newline:
Retrive Line Text from WPF Multiline TextBox
To retrieve text from a multiline TextBox in WPF, you simply access its Text property — just like you would with a single-line TextBox.
Get Textstring userInput =txtmulitiline.Text ;
This will retrive full text
Get Line Text // lineCount may be -1 if TextBox layout info is not up-to-date.
int lineCount = txtmulitiline.LineCount;
for (int line = 0; line < lineCount; line++)
{
// GetLineText takes a zero-based line index.
MessageBox.Show(txtmulitiline.GetLineText(line));
}
Related Articles
- WPF Textbox Numeric Only OR WPF Numeric Textbox
- Round Corner TextBox in WPF
- Round Corner TextBox With Border Effect
- Data Validation in WPF
Summary
To enable multiline functionality in a WPF TextBox, you mainly need to set AcceptsReturn="True"
and TextWrapping="Wrap"
. Combine this with scroll bar settings and adequate size to create a user-friendly multiline input field. With these simple tweaks, your WPF application can effectively handle complex user input.
Thanks