title: Is it better to put my logic in an event handler or in a setter for MVVM (Xamarin `Picker` `SelectedItem` quirks)
body: <p><strong>SOLUTION IS IN EDIT OF THE ACCEPTED ANSWER</strong></p>
  
  <p>I have a view in which has two Pickers, I need to have it so that when the <code>SelectedItem</code> property in one Picker changes, the list of Items in the second <code>Picker</code> (ItemSource) changes as well. </p>
  
  <p>Currently I have a bound the <code>SelectedItem</code> and <code>SelectedIndex</code> properties of both pickers to properties in my <code>ViewModel</code>. In the setter(s) for both of them, I perform the logic needed to change the list of Items in the second picker. The list of Items in the second picker changes successfully, but when I set the <code>SelectedIndex</code> (to make it select an Item by default), this fails if the index which I am setting it to is the same as the index which it was on in the previous list. It just shows the <code>Title</code> of the Picker instead, this issue seems to be related to <a href="https://github.com/xamarin/Xamarin.Forms/issues/1879" rel="nofollow noreferrer">this bug</a>.</p>
  
  <p>My Code: </p>
  
  <ul>
  <li>Both Pickers are bound to an Observable collection of strings FYI</li>
  <li><code>FrameType</code> and <code>DirectionType</code> are <code>Enums</code></li>
  <li>I initially used only the <code>SelectedItem</code> property</li>
  </ul>
  
  <p>Relevant XAML</p>
  
  <pre><code>&lt;Label Grid.Row="1" Grid.Column="0" VerticalTextAlignment="Center"
       Text="Direction: " /&gt;
       &lt;Picker x:Name="PickerDirection" Grid.Row="1" Grid.Column="1"
                        Title="Select Direction"
                        ItemsSource="{Binding Directions}"
                        SelectedItem="{Binding SelectedDirection}"
                        SelectedIndex="{Binding SelectedDirectionIndex}"&gt;&lt;/Picker&gt;
  
  &lt;Label Grid.Row="2" Grid.Column="0" VerticalTextAlignment="Center"
         Text="Frame: "/&gt;
  &lt;Picker x:Name="PickerFrame" Grid.Row="2" Grid.Column="1"
                   Title="Select Frame"
                   ItemsSource="{Binding Frames}"
                   SelectedItem="{Binding SelectedFrame}"
                   SelectedIndex="{Binding SelectedFrameIndex}"&gt;&lt;/Picker&gt;
  
  </code></pre>
  
  <p>Relevant View Model code:</p>
  
  <pre><code>public string SelectedDirection
  {
      get { return _selectedDirection; }
      set 
      {
          if (Directions.Contains(value))
          {
              if (_selectedDirection != value)
              {
                  if (EnumUtils.ToEnumFromString&lt;FrameType&gt;(SelectedFrame) == FrameType.Road &amp;&amp;
                              !DirectionsRoad.Contains(value))
                  {
                   _selectedDirection = Directions[Directions.IndexOf(DirectionType.Right.ToString())];
                  }
                  else
                  {
                      _selectedDirection = value;
                  }
                  OnPropertyChanged();
              }
          }
      }
  }
  
  public string SelectedFrame
  {
      get { return _selectedFrame; }
      set
      {
          if (_selectedFrame != value)
          {
              _selectedFrame = value;
              if (EnumUtils.ToEnumFromString&lt;FrameType&gt;(_selectedFrame) == FrameType.Road)
              {
                  Directions = DirectionsRoad;
                  if (Directions.Contains(SelectedDirection))
                  {
                      SelectedDirectionIndex = Directions.IndexOf(SelectedDirection);
                  }
                  else
                  {
                       SelectedDirectionIndex = Directions.IndexOf(DirectionType.Right.ToString());
                  }
                }else if (EnumUtils.ToEnumFromString&lt;FrameType&gt;(_selectedFrame) == FrameType.Lane)
                {
                    Directions = DirectionsAll;
                    SelectedDirectionIndex = Directions.IndexOf(SelectedDirection);
                }
                 OnPropertyChanged();
             }
         }
     }
  }
  
  public int SelectedDirectionIndex
  {
      get { return _selectedDirectionIndex; }
      set
      {
          if (_selectedDirectionIndex != value)
          {
              if (!(value &lt; 0 || value &gt;= Directions.Count))
              {
                  _selectedDirectionIndex = value;
                  OnPropertyChanged();
              }
          }
      }
  }
  
  public int SelectedFrameIndex
  {
      get { return _selectedFrameIndex; }
      set
      {
          if (_selectedFrameIndex != value)
          {
              if (!(value &lt; 0 || value &gt;= Frames.Count))
              {
                  _selectedFrameIndex = value;
                  OnPropertyChanged();
              }
          }
      }
  }
  
  
  </code></pre>
  
  <p>The outcome:
  <a href="https://i.stack.imgur.com/uGbZo.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uGbZo.gif" alt="enter image description here"></a></p>
  
  <p>I expect it to never be empty since I ensure that the SelectedDirection is always set to something.</p>
  
  <p><strong>Notes:</strong></p>
  
  <ul>
  <li><p>I initially used just the <code>SelectedItem</code> property, but when I encountered this bug I thought using the <code>SelectedIndex</code> property would help to fix it.</p></li>
  <li><p>I used <code>ObservableCollection</code> to maintain consistency with the other viewModels in the project, and to ensure that the options in the picker are updated when I make changes (based on my understanding you need to use ObservableCollection to make this possible).</p></li>
  <li><p>I do plan to refactor the code in the setter for <code>SelectedFrame</code> into smaller functions as soon as I get things to work.</p></li>
  </ul>
  
  <p>Due to this It seems that using the <code>SelectedIndexChanged</code> event of the <code>Picker</code> would be the only way to fix this. However <a href="https://stackoverflow.com/questions/59144365/how-do-clicked-and-command-work-when-they-are-both-used-on-the-same-xamarin">the comment by <strong>ottermatic</strong> in this question says that events are unreliable</a>. Hence I felt is was better to perform this logic in the setter.</p>
  
  <p>If someone could comment on what I may be doing wrong in my code which is causing this issue and also comment on the pros/cons and/or whether or not I should use the <code>eventHandler</code> or have the logic in my setter. Thanks</p>