title: How to bind view model to a UserControl in MVVMCROSS?
body: <p>I am using the excellent Mvvmcross and Ninja Coder for Mvvmcross for building a cross platform app. For my windows store app I have created a view and a view model using Ninja coder. I have also created a UserControl which will be referenced in the view. Hence I need to bind the same viewmodel to the User control also. I have been trying to set the Data context of the user control to the singleton instance of viewmodel. I have set the data context of the user control like below.</p>
  
  <pre><code>public sealed partial class SearchResultsGridViewControl : UserControl
  {
      private SearchresultsViewModel _viewModel;
  
      public SearchResultsGridViewControl()
      {
          this.InitializeComponent();
  
          _viewModel = Mvx.IocConstruct&lt;SearchresultsViewModel&gt;();
  
          this.DataContext = _viewModel;
      }
  }
  </code></pre>
  
  <p>But when I refer this User Control in my main view, it throws an error in XAML saying "Object Reference not set to an instance of an object. Cannot create an instance of SearchResultsGridViewControl".</p>
  
  <p>This is my viewmodel:</p>
  
  <pre><code>    public class SearchresultsViewModel : BaseViewModel
  {
      private ISearchResultsService _searchResultsService;
  
      public SearchresultsViewModel(ISearchResultsService searchResultsService)
      {
          _searchResultsService = searchResultsService;
          var items = _searchResultsService.DisplaySearchResults();
          SchoolDetails = new ObservableCollection&lt;School&gt;(items);            
      }
  
      private ObservableCollection&lt;School&gt; _schoolDetails;
  
      public ObservableCollection&lt;School&gt; SchoolDetails
      {
          get { return _schoolDetails; }
          set
          {
              _schoolDetails = value;
              RaisePropertyChanged(() =&gt; SchoolDetails);
          }
      }
  
      public ICommand RefineCommand
      {
          get
          {
              refineCommand = refineCommand ?? new MvxCommand(FilterSearchResultsBasedOnRefine);
              return refineCommand;
          }
      }
  
      public void FilterSearchResultsBasedOnRefine()
      {           
          SchoolDetails = new ObservableCollection&lt;School&gt;(_searchResultsService.FilterSchoolsBasedOnRefine(MidDayMeals, PlayGround, DigitalClassroom, DayBoarding, TransportationFacility));
      }
  }
  </code></pre>
  
  <p>The grid view in my usercontrol is getting populated when it loads for the first time. But when RefineCommand is called to update the collection from the main view, the grid view in usercontrol is not getting updated. And I am guessing its because of that error earlier in setting the data context of the user control to view model. Please let me know what could be going wrong. I have been banging my head about it for days.</p>