title: .NET Maui MVVM Picker Binding SelectedIndexChanged Event
body: <p>I am developing a Windows desktop app with .NET Maui using MVVM.</p>
  <p>I have 2 <code>Pickers</code> with <code>ImageSource</code> and <code>SelectedIndex</code> bound to properties in a viewmodel. When an item is selected from the first  <code>Picker</code> the items in the other need to be changed. I would like to bind the <code>SelectedIndexChanged</code> event to a method in the viewmodel to accomplish this.</p>
  <p>The XAML for the event in the picker <code>Picker</code> looks like this:</p>
  <pre><code>&lt;Picker SelectedIndexChanged=&quot;{Binding OnSelectedIndexChanged}&quot; /&gt;
  </code></pre>
  <p>The method in the viewmodel looks like this:</p>
  <pre><code>public void OnSelectedIndexChanged(object sender, EventArgs e)
  {
      // do stuff
  }
  </code></pre>
  <p>But I get the following error when running the program:</p>
  <blockquote>
  <p>XFC0009   No property, BindableProperty, or event found for &quot;SelectedIndexChanged&quot;, or mismatching type between value and property.   MauiApp1</p>
  </blockquote>
  <p>My temporary solution is to call the viewmodel method from the code behind when the event fires. The code behind looks like this:</p>
  <pre><code>private void Picker_SelectedIndexChanged(object sender, EventArgs e)
  {
      (BindingContext as MainViewModel).OnSelectedIndexChanged(sender, e);
  }
  </code></pre>
  <p>I would like to keep the code behind as dumb as possible. <strong>Is there a way to handle the <code>SelectedIndexChanged</code> event by binding directly to a method in the viewmodel?</strong></p>
  <p><strong>Update</strong> Trying to implement <code>partial void On&lt;PropertyName&gt;Changed()</code></p>
  <p>My viewmodel:</p>
  <pre><code>public partial class MainViewModel : ObservableObject
  {
      [ObservableProperty]
      private ObservableCollection&lt;ProductGroupRoot&gt; itemSourceProductGroups = new();
  
      [ObservableProperty]
      private int selectedProductGroup = -1;
  
      [ObservableProperty]
      private ObservableCollection&lt;ProductRoot&gt; itemSourceProducts = new();
  
      [ObservableProperty]
      private int selectedProduct = -1;
      
      // other properties
  
      partial void OnSelectedProductGroupChanged(int value)
      {
          // TODO: change values in ItemSourceProducts
      }        
  }
  </code></pre>
  <p>The autogenerated code should have created a definition for the partial method, but I get the error:</p>
  <blockquote>
  <p>CS0759    No defining declaration found for implementing declaration of partial method 'MainViewModel.OnSelectedProductGroupChanged(int)'</p>
  </blockquote>
  <p>I am using CommunityToolkit.Mvvm v7.1.2 (latest stable).</p>
  <p><strong>Update 2</strong>
  Posting the working code.</p>
  <p>My csproj file:</p>
  <pre><code>&lt;Project&gt;
      &lt;ItemGroup&gt;
          &lt;PackageReference Include=&quot;CommunityToolkit.Mvvm&quot; Version=&quot;8.0.0-preview4&quot; /&gt;
      &lt;/ItemGroup&gt;
  &lt;/Project&gt;
  </code></pre>
  <p>My Picker:</p>
  <pre><code>&lt;Picker ItemsSource=&quot;{Binding ProductGroups, Mode=TwoWay}&quot; 
          SelectedIndex=&quot;{Binding ProductGroupsIndex, Mode=TwoWay}&quot; /&gt;
  </code></pre>
  <p>My ViewModel:</p>
  <pre><code>[ObservableProperty]
  private ObservableCollection&lt;ProductGroupRoot&gt; productGroups = new();
  
  [ObservableProperty]
  private int productGroupsIndex = -1;
  
  partial void OnProductGroupsIndexChanged(int value) {}
  </code></pre>