Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make WPF TextBox with a scrollbar automatically scroll to the bottom when lines are added?

Tags:

.net

scroll

wpf

For example like Visual Studio's "Output" window does.

Is there a way to do it in XAML?

like image 588
Igor Avatar asked Aug 10 '11 12:08

Igor


People also ask

How do I scroll a text box in C#?

// Creating textbox TextBox Mytextbox = new TextBox(); Step 2 : After creating TextBox, set the ScrollBars property of the TextBox provided by the TextBox class. // Set ScrollBars property Mytextbox. ScrollBars = ScrollBars.

What is the difference between TextBox and TextBlock in WPF?

Text inside a TextBlock can not be made selectable by the user. TextBoxes are used for displaying text more focused for content input or when content is needed to be made selectable by the user. The TextBox can only be set to one colour, one font size, one font type etc.

How do I add a ScrollBar in WPF?

There are two predefined elements that enable scrolling in WPF applications: ScrollBar and ScrollViewer. The ScrollViewer control encapsulates horizontal and vertical ScrollBar elements and a content container (such as a Panel element) in order to display other visible elements in a scrollable area.


2 Answers

You can whenever you add content to that TextBox or when you listen to the event TextChanged fire this method: TextBoxBase.ScrollToEnd().

like image 166
Skomski Avatar answered Sep 24 '22 09:09

Skomski


You could write an attached property or even better a behavior that listens to the TextChanged event and scrolls to the bottom in the callback.

like image 43
bitbonk Avatar answered Sep 24 '22 09:09

bitbonk