WPF TextBox multiline text box

When ENTER is pressed, it looks like a normal [text box] , but can wrap to continue typing, use the AcceptsReturn and TextWrapping properties for multi-line text boxes, Height=”Auto” height auto.

First look at the multi-line text box effect:

When the AcceptsReturn property is set, the Enter Key is set by AcceptsReturn, and the control KeyDown event is set. When it is judged that the Enter key is pressed, the current operation is canceled.

<TextBox Grid.Row="6" HorizontalAlignment="Center" Height="Auto" Margin="10,10,0,0" 
             TextWrapping="Wrap" VerticalAlignment="Top" 
             Width="497" AcceptsReturn="True" 
             KeyDown="TextBoxKeyDown" />

private void TextBoxKeyDown(object sender, KeyEventArgs e)
            var textbox = sender as TextBox;
            if (e.Key == Key.Enter &&
                !(Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) &&
                !(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.LeftShift)))
                textbox.Text = "";
                e.Handled = true;

Recommend a [WPF] MVVM framework open source project: Newbeecoder.UI

Newbeecoder.UI open source project

Demo download:

Leave a Comment

Your email address will not be published. Required fields are marked *