Windows Phone. ObservableCollection as a source for ListView in Chat application

105 views Asked by At

I've ported an IRC library to WinRT (it supports a lot of RFC 1459 and a little of 2812) and now I try to use it.

As a view for IRC messages I've selected a ListView(don't know if it is a good idea) and RichTextBlock as Items.

I'm using an ObservableCollection to store incoming message(a class). It is binding well and even scrolls to bottom when new message arrives(thank you Marco Minerva for Behavior!).

I also sure that I need to keep a collection at some limit(like 100 messages). So I use a RemoveAt(0) at collection if counter is reached.

The problem is that when RemoveAt notifies the ListView it jumps to center of the message list(not to bottom).

Am I doing something wrong?

Thank you!

1

There are 1 answers

0
MeSaNei On BEST ANSWER

The solution is:

collection.CollectionChanged += (s, args) =>
            {
                var scrollViewer = behavior.AssociatedObject.GetFirstDescendantOfType<ScrollViewer>();
                scrollViewer.UpdateLayout();
                scrollViewer.Measure(scrollViewer.RenderSize);
                scrollViewer.ChangeView(0, scrollViewer.ScrollableHeight, scrollViewer.ZoomFactor, false);
            };