title: Autoscroll ListView in WPF using MVVM
body: <p>I try to have a list that automatically scrolls to the end when adding a new line.</p>
  
  <p>Here is a simple MVVM example in which I want to integrate :</p>
  
  <p>There is a button that adds lines to the list when clicked.</p>
  
  <p>Model code : </p>
  
  <pre><code>public class Student
  {
      public string Lastname {get; set;}
      public string Firstname {get; set;}
  
      public Student(string lastname, string firstname) {
          this.Lastname = lastname;
          this.Firstname = firstname;
  
      }
  }
  
  public class StudentsModel: ObservableCollection&lt;Student&gt;
  {
      private static object _threadLock = new Object();
      private static StudentsModel current = null;
  
      public static StudentsModel Current {
          get {
              lock (_threadLock)
              if (current == null)
                  current = new StudentsModel();
  
              return current;
          }
      }
  
      private StudentsModel() {
  
          for (int i = 1; i &lt;= 50; i++)
          {
              Student aStudent = new Student("Student " + i.ToString(), "Student " + i.ToString());
              Add(aStudent);
          }
      }
  
      public void AddAStudent(String lastname, string firstname) {
          Student aNewStudent = new Student(lastname, firstname);
          Add(aNewStudent);
      }
  }
  </code></pre>
  
  <p>ViewModel code :</p>
  
  <pre><code>public class MainViewModel : ViewModelBase
  {
  
      public StudentsModel Students { get; set; }
  
      public MainViewModel()
      {
          Students = StudentsModel.Current;
      }
  
      private ICommand _AddStudent;
      public ICommand AddStudent
      {
          get
          {
              if (_AddStudent == null)
              {
                  _AddStudent = new DelegateCommand(delegate()
                  {
                      Students.AddAStudent("New Student lastname", "New Student firstname");
                  });
              }
  
              return _AddStudent;
          }
      }
  </code></pre>
  
  <p>View code :</p>
  
  <pre><code>&lt;Window x:Class="demo.Views.MainView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:c="clr-namespace:demo.Commands"&gt;
  &lt;Grid&gt;
      &lt;ListView  Grid.Row="2" BorderBrush="White" ItemsSource="{Binding Path=Students}"
                 HorizontalAlignment="Stretch"&gt;
          &lt;ListView.View&gt;
              &lt;GridView&gt;
                  &lt;GridViewColumn Header="Lastname" DisplayMemberBinding="{Binding Path=Lastname}" /&gt;
                  &lt;GridViewColumn Header="Firstname" DisplayMemberBinding="{Binding Path=Firstname}" /&gt;
  
              &lt;/GridView&gt;
          &lt;/ListView.View&gt;
      &lt;/ListView &gt;
      &lt;Button Content="Add" Command="{Binding AddStudent}" Margin="601.94,36.866,96.567,419.403" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>Thank you</p>