How to use SelectedIndexChanged or CommandParameter with CommunityToolkit.Mvvm

Bhuwan 1,096 Reputation points
2025-01-18T16:28:39.4233333+00:00

I am using CommunityToolkit.Mvvm with ObservableObject and ObservableProperty. In the MVVM pattern, how can I trigger the SelectedIndexChanged event for a Picker when an item is selected, or handle changes in an Entry when typing into the textbox?

[RelayCommand]
private void OnChanged()
{
}  
<Picker TitleColor="{StaticResource LabelPlaceHolder}"
                       ItemsSource="{Binding Countrys}"
                       ItemDisplayBinding="{Binding Name}"
                       SelectedItem="{Binding SelectedItemCountry,Mode=TwoWay}"                        SelectedIndexChanged="{Binding OnChanged}"
                      FontFamily="OpenSansSemibold"
                       FontSize="12">
Developer technologies | .NET | .NET MAUI
{count} votes

2 answers

Sort by: Most helpful
  1. Harry Vo (WICLOUD CORPORATION) 3,505 Reputation points Microsoft External Staff Moderator
    2025-09-01T07:12:53.14+00:00

    Hi @Bhuwan , Sorry to bother you! I'm posting this answer to explain why your previous code didn't work and also help others who might encounter the same issue.


    In the first snippet, you're binding OnChanged in SelectedIndexChanged:

    <Picker
        …
        SelectedIndexChanged="{Binding OnChanged}" />
    

    In XAML, event attributes (like SelectedIndexChanged="...") expect a code‑behind method name, not a data binding. You can’t bind an event to a ViewModel method.


    In the second snippet, you're using EventToCommandBehavior:

    <Picker x:Name="pickerPatrolType" …>
      <Picker.Behaviors>
        <toolkit:EventToCommandBehavior
            EventName="SelectedIndexChanged"
            Command="{Binding CheckPatrolTypeSelectedCommand}" />
      </Picker.Behaviors>
    </Picker> 
    

    Starting with CommunityToolkit.Maui v10, behaviors do not inherit the parent control’s BindingContext. Without explicitly setting it, the Command binding can’t resolve—so your command never fires.

    To fix this issue, set the behavior’s BindingContext to the control’s BindingContext:

    <toolkit:EventToCommandBehavior
        EventName="SelectedIndexChanged"
        BindingContext="{Binding Source={x:Reference pickerPatrolType}, Path=BindingContext}"
        Command="{Binding CheckPatrolTypeSelectedCommand}" />
    

    Here is a full example of how to implement a logic trigger when a Picker selection changes using MVVM toolkit.

    • View:
    <ContentPage
        xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
    
        <Picker x:Name="pickerPatrolType"
                ItemsSource="{Binding PicPatrolTypes}"
                ItemDisplayBinding="{Binding GUIValue}"
                SelectedItem="{Binding PicPatrolTypeSelectedItem}"
                SelectedIndex="{Binding PicPatrolTypeIndex, Mode=TwoWay}">
            <Picker.Behaviors>
                <toolkit:EventToCommandBehavior
                    EventName="SelectedIndexChanged"
                    BindingContext="{Binding Source={x:Reference pickerPatrolType}, Path=BindingContext}"
                    Command="{Binding PatrolTypeChangedCommand}"
                    CommandParameter="{Binding Source={x:Reference pickerPatrolType}, Path=SelectedItem}" />
            </Picker.Behaviors>
        </Picker>
    </ContentPage>
    
    • ViewModel
    using CommunityToolkit.Mvvm.ComponentModel;
    using CommunityToolkit.Mvvm.Input;
     
    public partial class MyViewModel : ObservableObject
    {
        [ObservableProperty] private int picPatrolTypeIndex = -1;
        [ObservableProperty] private object? picPatrolTypeSelectedItem;
     
        [RelayCommand]
        private void PatrolTypeChanged(object? selectedItem)
        {
            if (PicPatrolTypeIndex != -1)
            {
                // do stuff (e.g., update other bound collections)
            }
            else
            {
                // do other stuff
            }
        }
    }
    

    1 person found this answer helpful.

  2. Bhuwan 1,096 Reputation points
    2025-01-19T05:42:52.01+00:00
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.