nikeee
nikeee

Reputation: 10724

Extend Listbox or use alternative?

I am creating some kind of chat style application. The Listbox is getting filled by its ItemSource which ist a Binding to a List. "Message" is a custom class containing various properties. The ListBox contains a DataTemplate which is used to display the Message-Item properly.

I've got a few problems when I am using a ListBox:
1. The user can not select text.
2. The ListBox does not automatically scroll down when an item is getting added to the List.

I'd like to use something like a TextBox, but I just couldn't figure out how to use some kind of DataTemplate and bind the Text-Property to a List. Of course, the text should be formattable.

Is there anything I can do to solve my problem? I thought about extending the ListBox-Control to a Auto-Scrolling one, but this wouldn't solve my problem concerning text selection.

Upvotes: 3

Views: 2980

Answers (3)

nikeee
nikeee

Reputation: 10724

I found a solution. I copied the ItemTemplate and pasted it into a new ItemsControl-Control. After that I wrapped a ScrollViewer around the ItemsControl. I added an eventhandler to the ListChanged event. Inside the eventhandler I called the ScrollToBottom-Method of the ItemsControl-Wrapping ScrollViewer which Matěj Zábský posted. To solve the problem concerning text selection, I used a TextBox with specific properties changed. Any way to make a WPF textblock selectable?

Thank you, Rachel and Matěj.

Upvotes: 2

Matěj Zábský
Matěj Zábský

Reputation: 17272

You could certainly use DataTemplate and make each item in the ListBox a TextBox.

As for the "2. The ListBox does not automatically scroll down when an item is getting added to the List.", call ScrollToBottom on the ListBox's ScrollViewer (you can use LogicalTreeHelper to find it).

Upvotes: 1

ikirachen
ikirachen

Reputation: 77

May be you have to re-think and to use some kind of ReachTextBox plus TextBox for typing.

Upvotes: 0

Related Questions