3
votes

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.

3
Why not use an ItemsControl and implement your own stuff to auto-scroll it? I doubt you'll need to track SelectedItem, and without it you can select your Text (Providing you use Labels and not TextBlocks)Rachel

3 Answers

2
votes

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.

1
votes

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).

0
votes

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