title: Update Observable Collection When Item Added to Model C# wpf
body: <p>Ok so I'm fairly new to this. I followed along with <a href="https://www.youtube.com/watch?v=PUvZ5wVrZ4s" rel="nofollow">this MVVM tutorial</a> from YouTube. It was pretty good and straightforward. Basically it sets up a very basic program with a Model class, DataAcess class, 3 viewmodels (Main window, Employee and ViewModelBase) and finally a view which has a stackpanel and a couple of text boxes that are bound to the FirstName and LastName in the Model.</p>
  
  <p>It all works how it's meant to and I have been through it a number of times and I'm pretty sure I understand how it all works <strong>but the trouble that I am having is adding new Employees</strong>.</p>
  
  <p>In the DataAccess class (Employee Repository) Employees are added as shown below.</p>
  
  <pre><code>    class EmployeeRepository
  {
      readonly List&lt;Employee&gt; _employee;
  
      public EmployeeRepository()
      {
          if (_employee == null)
          {
              _employee = new List&lt;Employee&gt;();
          }
  
          _employee.Add(Employee.CreateEmployee("Bob", "Jones"));
          _employee.Add(Employee.CreateEmployee("Sarah", "Marshall"));
          _employee.Add(Employee.CreateEmployee("Peter", "Piper"));
  
      }
  
      public List&lt;Employee&gt; GetEmployees()
      {
          return new List&lt;Employee&gt;(_employee);
      }
  
  
  }
  </code></pre>
  
  <p>And in the Model there is a method call CreateEmployee as such</p>
  
  <pre><code>    public class Employee
  {
      public static Employee CreateEmployee(string firstName, string lastName)
      {
  
          return new Employee { FirstName = firstName, LastName = lastName };
  
      }
      public string FirstName { get; set; }
      public string LastName { get; set; }
  }
  </code></pre>
  
  <p>So I thought I would add a button to the MainWindow and then add another name to the list. Hopping the view would update as an item is updated. Just to see if it would work I just used the code behind.</p>
  
  <p>I thought I could just add a new employee the same way I did in the EmployeeRepository so I tried this</p>
  
  <pre><code>    readonly List&lt;Employee&gt; _employee = new List&lt;Employee&gt;();
  
      private void btnAdd_Click(object sender, RoutedEventArgs e)
      {
          _employee.Add(Employee.CreateEmployee("John", "Smith"));
      }
  </code></pre>
  
  <p>I have tried many many ways of doing this, to no avail. I have watched and read many tutorials and questions, but nothing that I have tried as worked.</p>
  
  <p>What am I missing? I initially thought that it was not working because I am adding the item to the List in the repository, but not to the ObservableCollection that is in the viewmodel. And the AllEmployees ObservableCollection is the ItemSource for view.</p>
  
  <pre><code>    readonly EmployeeRepository _employeeRepository;
  
      public ObservableCollection&lt;Model.Employee&gt; AllEmployees
      {
          get;
          private set;
      }
  
      public EmployeeListViewModel(EmployeeRepository currentWindowRepository)
      {
          if (currentWindowRepository == null)
          {
              throw new ArgumentException("currentWindowRepository");
          }
          _employeeRepository = currentWindowRepository;
          this.AllEmployees = new ObservableCollection&lt;Model.Employee&gt;(_employeeRepository.GetEmployees());
      }
  </code></pre>
  
  <p>But in the button code I tried to implement something similar, but no.</p>
  
  <p>I can also add the view xaml code and MainViewModel codes so that you can see how it's all bound if you like.</p>
  
  <p>Thanks in advance for any help!</p>