mauipickercollectionviewselectedindexchanged

Maui Picker in a CollectionView Item, Can't bind SelectedIndexChanged to the collection item object


I have cart items in a Grid inside a DataTemplate for showing CollectionView items. In the cart I have Add/Remove buttons with Commands that route to the CartViewModel and pass a CommandParameter of which CartViewItem they belong to.

Now I need to do the same with a picker for the pricing type drop down I can't figure out how to route the picker PropertyChanged to the model and pass along which item had its picker changed.

What's the best way to get the information I need on which cart item the PropertyChanged event is referring to? This code just sends the command to the CartPage Content Page with no info on which cart item it belongs to.

            <Picker Grid.Row="3" 
                    SelectedIndexChanged="Picker_SelectedIndexChanged" 
                    ItemsSource="{Binding CostTypeList}"
                    SelectedItem="{Binding CostType}"
                    />
            <Button Text="ADD" 
                        Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:CartViewModel}},Path=AddCommand}" 
                        CommandParameter="{Binding .}" 
                        Grid.Row="4" BackgroundColor="Blue"></Button>
            <Button Text="REMOVE" 
                        Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:CartViewModel}},Path=RemoveCommand}" 
                        CommandParameter="{Binding .}"  Grid.Row="4" Grid.Column="1" BackgroundColor="DarkRed"></Button>

And I can't get a syntax similar to the button syntax to work.

            <Picker Grid.Row="3" 
                    SelectedIndexChanged="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:CartViewModel}},Path=Picker_SelectedIndexChanged}"
                    ItemsSource="{Binding CostTypeList}"
                    SelectedItem="{Binding CostType}"
                    />

Gives this error before building

XFC0000 Cannot resolve type "clr-namespace:SVCA_Android.ViewModels:CartViewItem". sign-in-maui (net8.0-android34.0)

And this error after building

Error (active) XFC0009 No property, BindableProperty, or event found for "SelectedIndexChanged", or mismatching type between value and property. sign-in-maui (net8.0-android34.0)

public partial class CartViewModel : ObservableObject
{

    [ObservableProperty]
    ObservableCollection<CartViewItem> items;
    [ObservableProperty]
    public string costType;
    [ObservableProperty]
    public List<string> costTypeList;

    public partial class CartViewItem : ObservableObject
    {
        [ObservableProperty]
        public string costType;
        [ObservableProperty]
        public List<string> costTypeList;
    }

    public void Picker_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    [RelayCommand]
    async Task Add(CartViewItem i)
    {
        System.Diagnostics.Debug.WriteLine(i.ItemName + " " + i.ItemQty);
    }
    [RelayCommand]
    async Task Remove(CartViewItem i)
    {
        System.Diagnostics.Debug.WriteLine(i.ItemName + " " + i.ItemQty);
    }
}

Solution

  • Using Picker_SelectedIndexChanged was the wrong approach for the situation of getting picker changed information to a CollectionViews item.

    The correct approach using MVVM community toolkit is to use the OberservableProperty watchers that are generated by the toolkit. Note you need to use the uppercase of the ObservableProperty like this

        public partial class CartViewItem : ObservableObject
        {
            [ObservableProperty]
            public string costType;
            [ObservableProperty]
            public List<string> costTypeList;
    
            partial void OnCostTypeChanged(string value)
            {
                System.Diagnostics.Debug.WriteLine(value);
            }
    

    With xaml

                <Picker Grid.Row="3" 
                        ItemsSource="{Binding CostTypeList}"
                        SelectedItem="{Binding CostType}"
                        />
    

    Also be aware that at least on Android that watcher is going to fire once on the init of each cart item.

    Refer to Passing parameter from ViewModel to ViewModel in .NET MAUI using CommunityToolkit.MVVM