title: Asynchronous MVVM for WPF C# using MongoDB
body: <p>So my situation is this: I want to be able to use MVVM with my WPF application using MongoDB. I am very new to MVVM (I know very little of it), but I've got some experience using .NET and WPF.</p>
  
  <p>I have a namespace for recalling MongoDB collections, with the Model component stored there as a class called "User"</p>
  
  <p>Model (in a separate namespace):</p>
  
  <pre><code>public class User
  {
      [BsonElement("_id")]
      public ObjectId Id { get; set; }
      public string name { get; set; }
      // other methods listed here
  
      public async static Task&lt;List&lt;User&gt;&gt; getUserList()
      {
          // allows me to get a list of users
          var col = MongoDBServer&lt;User&gt;.openMongoDB("Users");
          var filter = Builders&lt;User&gt;.Filter.Exists("name");
  
          List&lt;User&gt; userList = await col.Find(filter).ToListAsync();
  
          return userList;
      }
  }
  </code></pre>
  
  <p>I've created a very basic ViewModelBase (abstract ViewModelBase):</p>
  
  <pre><code>public abstract class ViewModelBase : INotifyPropertyChanged
  {
      public event PropertyChangedEventHandler PropertyChanged;
  
      protected virtual void OnPropertyChanged(string propertyName)
      {
          PropertyChangedEventHandler handler = this.PropertyChanged;
          if(handler == null)
          {
              var e = new PropertyChangedEventArgs(propertyName);
              handler(this, e);
          }
      }
  }
  </code></pre>
  
  <p>And a derived class for handling the User Lists (ViewModel):</p>
  
  <pre><code>public class UserListViewModel : ViewModelBase
  {
      private User _user;
      private ObservableCollection&lt;User&gt; _userList;
  
      public User user
      {
          get { return _user; }
          set
          {
              _user = value;
              OnPropertyChanged("user");
          }
      }
  
      public ObservableCollection&lt;User&gt; userList
      {
          get { return _userList; }
          set
          {
              _userList = value;
              OnPropertyChanged("userList");
          }
      }
  
      public UserListViewModel()
      {
          user = new User();
          this.userList = new ObservableCollection&lt;User&gt;();
  
          // since MongoDB operations are asyncrhonous, the async method "getUserList()" is used to fill the observable collection
          getUserList().Wait();
      }
  
      public async Task getUserList()
      {
          var UserListRaw = await User.getUserList();
          this.userList = new ObservableCollection&lt;User&gt;(UserListRaw);
      }
  }
  </code></pre>
  
  <p>The view component is as a simple window with a listbox, as follows (View):</p>
  
  <pre><code>&lt;Window x:Class="UserManagementMVVM.UsersWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:local="clr-namespace:UserManagementMVVM"
      mc:Ignorable="d"
      Title="UsersWindow" Height="300" Width="300"&gt;
      &lt;Window.Resources&gt;
          &lt;local:UserListViewModel x:Key="ViewModel"/&gt;
          &lt;!-- Receiving error for this XAML block saying "Object reference not set to instance of an object --&gt;
      &lt;/Window.Resources&gt;
  
      &lt;Grid DataContext="{Binding ViewModel}"&gt;
          &lt;ListBox Margin="5" ItemsSource="{Binding userList}"/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>The App.Xaml and its codebehind are left untouched, as is the View's codebehind.</p>
  
  <p>When I run the program, nothing shows up (ie: The Window starts, but the ListBox is empty even though there is data). I will soon add some button functionality that will perform atomic operations with MongoDB.</p>
  
  <p>I've been trying for nearly 2 weeks to make my own MVVM program for this, with no success. Any assistance would be greatly appreciated.</p>