chuckd
chuckd

Reputation: 14520

Scrolling text inside a textbox

I have a WPF app with a textbox that I'm sending text to throughout the run cycle of the app. As I'm feedign data to the text box to give updates from the app I want the text inside the textbox to scroll down with the data. Right now the scroll bar stays at the top of the textbox and I have to scroll down to see what has been inserted into the control. How do i eneable it so I don't have to scroll down to see the text?

Upvotes: 2

Views: 225

Answers (2)

kcnygaard
kcnygaard

Reputation: 814

There is a method on TextBox just for this: TextBox.ScrollToEnd().

Call that method whenever text is added to the TextBox.

Upvotes: 1

Noctis
Noctis

Reputation: 11763

Why don't you use a ListBox instead of a TextBox, and then Bind it to an ObservableCollection on your ViewModel.

public ObservableCollection<string> MyUpdates {get; set;}

Then, whenever you update a message (from your Model), you can just add it to the head of the collection:

MyUpdates.Insert(0, your_message_here); 

Your view is automatically refreshed and your new messages are on top.

(You can probably play with different containers for that, depending on what you want, and if you want a scrolbar or not. Have a look at this for examples)

Upvotes: 1

Related Questions