title: Scroll to new item in ListView for UWP
body: <p>I'm creating a chat application with a ListView that contains the messages. When a new message is sent/received, the ListView should scroll to the new message.</p>
  
  <p>I'm using MVVM, so the ListView looks like</p>
  
  <pre><code>&lt;ScrollViewer&gt;
      &lt;ItemsControl Source="{Binding Messages}" /&gt;
  &lt;/ScrollViewer&gt;
  </code></pre>
  
  <p>How can I do it?</p>
  
  <p>EDIT: I tried to make this work in versions prior to the Anniversary Update creating a Behavior. This is what I have so far:</p>
  
  <pre><code>public class FocusLastBehavior : Behavior&lt;ItemsControl&gt;
  {
      protected override void OnAttached()
      {
          base.OnAttached();
          AssociatedObject.Items.VectorChanged += ItemsOnVectorChanged;
      }
  
      private void ItemsOnVectorChanged(IObservableVector&lt;object&gt; sender, IVectorChangedEventArgs @event)
      {
          var scroll = VisualTreeExtensions.FindVisualAscendant&lt;ScrollViewer&gt;(AssociatedObject);
          if (scroll == null)
          {
              return;
          }
  
          var last = AssociatedObject.Items.LastOrDefault();
  
          if (last == null)
          {
              return;
          }
  
          var container = AssociatedObject.ContainerFromItem(last);
  
  
          ScrollToElement(scroll, (UIElement)container);
      }
  
      private static void ScrollToElement(ScrollViewer scrollViewer, UIElement element,
          bool isVerticalScrolling = true, bool smoothScrolling = true, float? zoomFactor = null)
      {
          var transform = element.TransformToVisual((UIElement)scrollViewer.Content);
          var position = transform.TransformPoint(new Point(0, 0));
  
          if (isVerticalScrolling)
          {
              scrollViewer.ChangeView(null, position.Y, zoomFactor, !smoothScrolling);
          }
          else
          {
              scrollViewer.ChangeView(position.X, null, zoomFactor, !smoothScrolling);
          }
      }
  }
  </code></pre>
  
  <p>The code uses VisualTreeExtensions from the <a href="http://www.uwpcommunitytoolkit.com/en/master/" rel="nofollow noreferrer">UWP Community Toolkit</a></p>
  
  <p>However, the position after the call to TransformPoint always returns <code>{0, 0}</code></p>
  
  <p>What am I doing wrong?</p>