title: UWP C#/XAML: System.ArgumentException (Out of expected range)
body: <p>I'm working on a Universal Windows Platform (UWP) app, and have run into a perplexing (And slightly annoying issue). When I navigate from the MainPage to EventDetailPage, a <code>System.ArgumentException</code>. It's indicating that the data bound object </p>
  
  <blockquote>
    <p>"Value does not fall within the expected range". </p>
  </blockquote>
  
  <p>Value is an ScoutingEvent object passed from MainPageViewModel to EventDetailPageViewModel. Although the passed object is sometimes null (not sure why), Value is also assigned default data to ensure it's not null when EventDetailPage attempts to bind to Bind to value.</p>
  
  <p>From my perspective, everything looks correct. Is there something I'm missing here?</p>
  
  <p><strong>MainPage.xaml</strong></p>
  
  <pre><code>&lt;Page x:Class="ScoutsLog.Views.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Behaviors="using:Template10.Behaviors"
    xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
    xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
    xmlns:controls="using:Template10.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:ScoutsLog.Views"
    xmlns:data="using:ScoutsLog.Models"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vm="using:ScoutsLog.ViewModels" mc:Ignorable="d"&gt;
  
      &lt;Page.DataContext&gt;
          &lt;vm:MainPageViewModel /&gt;
      &lt;/Page.DataContext&gt;
  
      &lt;Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"&gt;
  
          &lt;!--  #region default visual states  --&gt;
  
          &lt;VisualStateManager.VisualStateGroups&gt;
              &lt;VisualStateGroup x:Name="AdaptiveVisualStateGroup"&gt;
                  &lt;VisualState x:Name="VisualStateNarrow"&gt;
                      &lt;VisualState.StateTriggers&gt;
                          &lt;AdaptiveTrigger MinWindowWidth="{StaticResource NarrowMinWidth}" /&gt;
                      &lt;/VisualState.StateTriggers&gt;
                      &lt;VisualState.Setters&gt;
                          &lt;!--  TODO  --&gt;
                      &lt;/VisualState.Setters&gt;
                  &lt;/VisualState&gt;
                  &lt;VisualState x:Name="VisualStateNormal"&gt;
                      &lt;VisualState.StateTriggers&gt;
                          &lt;AdaptiveTrigger MinWindowWidth="{StaticResource NormalMinWidth}" /&gt;
                      &lt;/VisualState.StateTriggers&gt;
                      &lt;VisualState.Setters&gt;
                          &lt;!--  TODO  --&gt;
                      &lt;/VisualState.Setters&gt;
                  &lt;/VisualState&gt;
                  &lt;VisualState x:Name="VisualStateWide"&gt;
                      &lt;VisualState.StateTriggers&gt;
                          &lt;AdaptiveTrigger MinWindowWidth="{StaticResource WideMinWidth}" /&gt;
                      &lt;/VisualState.StateTriggers&gt;
                      &lt;VisualState.Setters&gt;
                          &lt;!--  TODO  --&gt;
                      &lt;/VisualState.Setters&gt;
                  &lt;/VisualState&gt;
              &lt;/VisualStateGroup&gt;
          &lt;/VisualStateManager.VisualStateGroups&gt;
  
          &lt;!--  #endregion  --&gt;
  
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto" /&gt;
              &lt;RowDefinition /&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;!--  page header  --&gt;
  
          &lt;controls:PageHeader BackButtonVisibility="Collapsed" Content="Main Page" Frame="{x:Bind Frame}"&gt;
              &lt;Interactivity:Interaction.Behaviors&gt;
                  &lt;Behaviors:EllipsisBehavior Visibility="Auto" /&gt;
              &lt;/Interactivity:Interaction.Behaviors&gt;
              &lt;controls:PageHeader.SecondaryCommands&gt;
                  &lt;AppBarButton Click="{x:Bind ViewModel.GotoPrivacy}" Label="Privacy" /&gt;
                  &lt;AppBarButton Click="{x:Bind ViewModel.GotoAbout}" Label="About" /&gt;
              &lt;/controls:PageHeader.SecondaryCommands&gt;
          &lt;/controls:PageHeader&gt;
  
          &lt;!--  page content  --&gt;
  
          &lt;!--&lt;StackPanel Grid.Row="1" VerticalAlignment="Top"
                      Orientation="Horizontal" Padding="12,8,0,0"&gt;
  
              &lt;controls:Resizer&gt;
                  &lt;TextBox Width="200" MinWidth="200"
                           MinHeight="60" Margin="0"
                           Header="Parameter to pass"
                           Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                           TextWrapping="Wrap"&gt;
                      &lt;Interactivity:Interaction.Behaviors&gt;
                          &lt;Behaviors:TextBoxEnterKeyBehavior&gt;
                              &lt;Core:CallMethodAction MethodName="GotoDetailsPage" TargetObject="{Binding}" /&gt;
                          &lt;/Behaviors:TextBoxEnterKeyBehavior&gt;
                          &lt;Core:EventTriggerBehavior&gt;
                              &lt;Behaviors:FocusAction/&gt;
                          &lt;/Core:EventTriggerBehavior&gt;
                      &lt;/Interactivity:Interaction.Behaviors&gt;
                  &lt;/TextBox&gt;
              &lt;/controls:Resizer&gt;
  
              &lt;Button Margin="12,0" VerticalAlignment="Bottom"
                      Click="{x:Bind ViewModel.GotoDetailsPage}" Content="Submit" /&gt;
  
          &lt;/StackPanel&gt;--&gt;
  
          &lt;ListBox Grid.Row="1" VerticalAlignment="Top" Padding="12,8,0,0" Name="EventsOverviewListBox" 
                   ItemsSource="{x:Bind ViewModel.Events}" SelectionChanged="{x:Bind ViewModel.GotoEventDetailsPage}"
                   SelectedIndex="{Binding EventsIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"&gt;
              &lt;ListBox.ItemTemplate&gt;
                  &lt;DataTemplate x:DataType="data:ScoutingEvent"&gt;
                      &lt;TextBlock Text="{x:Bind EventName}"/&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListBox.ItemTemplate&gt;
          &lt;/ListBox&gt;
      &lt;/Grid&gt;
  &lt;/Page&gt;
  </code></pre>
  
  <p><strong>MainPageViewModel.cs</strong></p>
  
  <pre><code>using ScoutsLog.Models;
  using ScoutsLog.Utils;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using Windows.Storage;
  using Windows.UI.Core;
  using Windows.UI.Xaml.Navigation;
  
  namespace ScoutsLog.ViewModels
  {
      public class MainPageViewModel : Mvvm.ViewModelBase
      {
          Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
          public List&lt;ScoutingEvent&gt; Events;
  
          public MainPageViewModel()
          {
              //if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
              //    Value = "Designtime value";
  
              // Register a handler for BackRequested events and set the
              // visibility of the Back button
              SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
  
              ReadDataFromStorage();
  
              //if (Events == null || Events.Count &lt;= 0)
              //{
                  Events = EventsManager.GetEvents();
              //}
  
          }
  
          private async void ReadDataFromStorage()
          {
              try
              {
                  StorageFile sampleFile = await localFolder.GetFileAsync("dataFile.txt");
                  String listAsXml = await FileIO.ReadTextAsync(sampleFile);
                  Events = XmlHandler.DeserializeXmlToList(listAsXml);
              }
              catch(Exception e)
              {
                  System.Diagnostics.Debug.WriteLine(e);
              }
          }
  
          private async void OnBackRequested(object sender, BackRequestedEventArgs e)
          {
              StorageFile sampleFile = await localFolder.CreateFileAsync("dataFile.txt",
              CreationCollisionOption.ReplaceExisting);
              await FileIO.WriteTextAsync(sampleFile, XmlHandler.SerializeListToXml(Events));
  
          }
  
          ScoutingEvent _Value = new ScoutingEvent();
          public ScoutingEvent Value { get { return _Value; } set { Set(ref _Value, value); } }
  
          int _EventsIndex = 0;
          public int EventsIndex { get { return _EventsIndex; } set { Set(ref _EventsIndex, value); } }
  
          public override void OnNavigatedTo(object parameter, NavigationMode mode, IDictionary&lt;string, object&gt; state)
          {
              if (state.ContainsKey(nameof(Value)))
                  Value = (ScoutingEvent)state[nameof(Value)];
              state.Clear();
          }
  
          public override async Task OnNavigatedFromAsync(IDictionary&lt;string, object&gt; state, bool suspending)
          {
              if (suspending)
                  state[nameof(Value)] = Value;
              await Task.Yield();
          }
  
          public void GotoEventDetailsPage()
          {
              Value = Events.ElementAt(EventsIndex);
              NavigationService.Navigate(typeof(Views.EventDetailPage), Value);
          }
  
          public void GotoPrivacy()
          {
              NavigationService.Navigate(typeof(Views.SettingsPage), 1);
          }
  
          public void GotoAbout()
          {
              NavigationService.Navigate(typeof(Views.SettingsPage), 2);
          }
  
      }
  }
  </code></pre>
  
  <p><strong>EventDetailPage.xaml</strong></p>
  
  <pre><code>&lt;Page
  x:Class="ScoutsLog.Views.EventDetailPage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:Behaviors="using:Template10.Behaviors"
  xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
  xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
  xmlns:controls="using:Template10.Controls"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:local="using:ScoutsLog.Views"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:vm="using:ScoutsLog.ViewModels" x:Name="ThisPage"
  xmlns:data="using:ScoutsLog.Models"
  mc:Ignorable="d"&gt;
  
      &lt;Page.DataContext&gt;
          &lt;vm:EventDetailsPageViewModel /&gt;
      &lt;/Page.DataContext&gt;
  
      &lt;Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"&gt;
  
          &lt;!--  adaptive states  --&gt;
  
          &lt;VisualStateManager.VisualStateGroups&gt;
              &lt;VisualStateGroup x:Name="AdaptiveVisualStateGroup"&gt;
                  &lt;VisualState x:Name="VisualStateNarrow"&gt;
                      &lt;VisualState.StateTriggers&gt;
                          &lt;AdaptiveTrigger MinWindowWidth="{StaticResource NarrowMinWidth}" /&gt;
                      &lt;/VisualState.StateTriggers&gt;
                      &lt;VisualState.Setters&gt;
                          &lt;!--  TODO  --&gt;
                      &lt;/VisualState.Setters&gt;
                  &lt;/VisualState&gt;
                  &lt;VisualState x:Name="VisualStateNormal"&gt;
                      &lt;VisualState.StateTriggers&gt;
                          &lt;AdaptiveTrigger MinWindowWidth="{StaticResource NormalMinWidth}" /&gt;
                      &lt;/VisualState.StateTriggers&gt;
                      &lt;VisualState.Setters&gt;
                          &lt;!--  TODO  --&gt;
                      &lt;/VisualState.Setters&gt;
                  &lt;/VisualState&gt;
                  &lt;VisualState x:Name="VisualStateWide"&gt;
                      &lt;VisualState.StateTriggers&gt;
                          &lt;AdaptiveTrigger MinWindowWidth="{StaticResource WideMinWidth}" /&gt;
                      &lt;/VisualState.StateTriggers&gt;
                      &lt;VisualState.Setters&gt;
                          &lt;!--  TODO  --&gt;
                      &lt;/VisualState.Setters&gt;
                  &lt;/VisualState&gt;
              &lt;/VisualStateGroup&gt;
          &lt;/VisualStateManager.VisualStateGroups&gt;
  
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto" /&gt;
              &lt;RowDefinition /&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;!--  header  --&gt;
          &lt;controls:PageHeader Frame="{x:Bind Frame}" Text="Detail Page"&gt;
              &lt;Interactivity:Interaction.Behaviors&gt;
                  &lt;Behaviors:EllipsisBehavior Visibility="Auto" /&gt;
              &lt;/Interactivity:Interaction.Behaviors&gt;
          &lt;/controls:PageHeader&gt;
  
          &lt;GridView Grid.Row="1" VerticalAlignment="Top" Padding="12,8,0,0" Name="EventsOverviewGridView" ItemsSource="{x:Bind ViewModel.Value}"&gt;
              &lt;GridView.ItemTemplate&gt;
                  &lt;DataTemplate x:DataType="data:ScoutingEvent"&gt;
                      &lt;Grid&gt;
                          &lt;StackPanel Grid.Row="0"&gt;
                              &lt;TextBlock Text="{x:Bind EventName}" /&gt;
                              &lt;ListBox ItemsSource="{x:Bind Companies}"&gt;
                                  &lt;ListBox.ItemTemplate&gt;
                                      &lt;DataTemplate x:DataType="data:Company"&gt;
                                          &lt;TextBlock Text="{x:Bind CompanyName}"/&gt;
                                      &lt;/DataTemplate&gt;
                                  &lt;/ListBox.ItemTemplate&gt;
                              &lt;/ListBox&gt;
                          &lt;/StackPanel&gt;
                      &lt;/Grid&gt;
                  &lt;/DataTemplate&gt;
              &lt;/GridView.ItemTemplate&gt;
          &lt;/GridView&gt;
  
          &lt;!--  #endregion  --&gt;
  
      &lt;/Grid&gt;
  &lt;/Page&gt;
  </code></pre>
  
  <p><strong>EventDetailPageViewModel.cs</strong></p>
  
  <pre><code>using ScoutsLog.Models;
  using System.Linq;
  using System.Collections.Generic;
  using System.Threading.Tasks;
  using Windows.UI.Xaml.Navigation;
  
  namespace ScoutsLog.ViewModels
  {
      public class EventDetailsPageViewModel : Mvvm.ViewModelBase
      {
          public EventDetailsPageViewModel()
          {
  
          }
  
          ScoutingEvent _Value = new ScoutingEvent { EventName = string.Empty, EventDate = string.Empty, Companies = new List&lt;Company&gt;()};
          public ScoutingEvent Value { get { return _Value; } set { Set(ref _Value, value); } }
  
          public override void OnNavigatedTo(object parameter, NavigationMode mode, IDictionary&lt;string, object&gt; state)
          {
              if (state.ContainsKey(nameof(Value)))
                  Value = (ScoutingEvent)state[nameof(Value)];
              state.Clear();
          }
  
          public override async Task OnNavigatedFromAsync(IDictionary&lt;string, object&gt; state, bool suspending)
          {
              if (suspending)
                  state[nameof(Value)] = Value;
              await Task.Yield();
          }
  
          public void GotoDetailsPage()
          {
              NavigationService.Navigate(typeof(Views.EventDetailPage), Value);
          }
  
          public void GotoPrivacy()
          {
              NavigationService.Navigate(typeof(Views.SettingsPage), 1);
          }
  
          public void GotoAbout()
          {
              NavigationService.Navigate(typeof(Views.SettingsPage), 2);
          }
      }
  }
  </code></pre>
  
  <p><strong>Update 1: Adding ScoutingEvent.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  
  namespace ScoutsLog.Models
  {
      public class ScoutingEvent : INotifyPropertyChanged
      {
          private string eventName;
           public string EventName
           {
              get
              {
                  return eventName;
              }
                 set
              {
                  if (eventName != value)
                  {
                      eventName = value;
                      OnNotifyPropertyChanged("EventName");
                  }
              }
          }
  
      private string eventDate;
      public string EventDate
      {
          get
          {
              return eventDate;
          }
          set
          {
              if (eventDate != value)
              {
                  eventDate = value;
                  OnNotifyPropertyChanged("EventDate");
              }
          }
      }
  
      public List&lt;Company&gt; Companies { get; set; }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      private void OnNotifyPropertyChanged(string propertyName)
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  }
  
  public class Company
  {
      public string CompanyName { get; set; }
      public string Category { get; set; }
      public Boolean isRelevent { get; set; }
      public String DateVisited { get; set; }
      public string Notes { get; set; }
  }
  
  public class EventsManager
  {
      public static List&lt;ScoutingEvent&gt; GetEvents()
      {
          var events = new List&lt;ScoutingEvent&gt;();
          var companies = GetCompanies();
  
          events.Add(new ScoutingEvent { EventName = "CES", EventDate = "1/6/2016 - 1/9/2016", Companies = companies});
          events.Add(new ScoutingEvent { EventName = "MWC", EventDate = "3/3/2016 - 3/10/2016", Companies = companies });
  
          return events;
      }
  
      public static List&lt;Company&gt; GetCompanies()
      {
          var companies = new List&lt;Company&gt;();
          companies.Add(new Company { CompanyName = "Spire", Category = "Fittness", DateVisited = "1/6/2016", isRelevent = true, Notes = "Follow-up after event." });
          companies.Add(new Company { CompanyName = "Samsung", Category = "Fittness", DateVisited = "1/8/2016", isRelevent = false, Notes = "Not working on relevent technologies." });
          companies.Add(new Company { CompanyName = "Fitbit", Category = "Fittness", DateVisited = "1/7/2016", isRelevent = true, Notes = "Pass along to remote team." });
          companies.Add(new Company { CompanyName = "Home.io", Category = "Fittness", DateVisited = "1/9/2016", isRelevent = true, Notes = "SHare with Paul/Martta." });
          return companies;
      }
  }
  }
  </code></pre>