Search code examples
c#maui

Issue with selecteditem in collectionview's SelectionChangedCommand


I have added SelectionChangedCommand and SelectedItem commands for collectionview.

<CollectionView
        Margin="0,10,0,0"
        BackgroundColor="Transparent"
        IsVisible="{Binding Visibility}"
        ItemsSource="{Binding ImageItems}"
        ItemsLayout="VerticalGrid, 2"
        SelectionMode="Single"
        SelectionChangedCommand="{Binding ImageItemTappedCommand}"
        SelectedItem="{Binding LastImageTappedItem, Mode=TwoWay}"
        HeightRequest="{Binding ImageHeight}">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <StackLayout BackgroundColor="{Binding ImageBGColor}">

ImageItemTappedCommand:

public NameMatchViewModel() {
    ImageItemTappedCommand = new Command((obj) = >{
        try {
            //reset the bg color 
            foreach(var item in ImageItems) {
                item.ImageBGColor = Colors.White;
            }
            NameMatchList imageList = obj as NameMatchList;
            if (imageList != null) {
                Debug.WriteLine("**not null**");
            }
            else {
                Debug.WriteLine("**null**");
            }
            int index = ImageItems.IndexOf(imageList);
            imageList.ImageBGColor = Color.FromArgb("#0091da");
            //Storing name and imageurl to local db
            if (Utility.IsInternet()) {
                Preferences.Default.Set("NameMatchImageList_Image", imageList.imageUrl);
            }
            else {
                Preferences.Default.Set("NameMatchImageList_Image", imageList.FullImageUrl);
            }
            Preferences.Default.Set("NameMatchImageList_Name", imageList.name);
            Preferences.Default.Set("ImageItem", imageList);
            isImageSelected = true;
            if (isImageSelected && isNameSelected) {
                //If both image and name selected by player startes checking the matching
                StartNameMatchCheck(imageList);
            }
        }
        catch(Exception imagetapEx) {
            Debug.WriteLine("imagetapEx:>>" + imagetapEx);
        }
    });
}

But when I try to fetch the selecteditem value I am getting null. For the above code I am getting below exception:

Exception is for the below line:

imageList.ImageBGColor = Color.FromArgb("#0091da");
**null**
12:30:36:070    [0:] imagetapEx:>>System.NullReferenceException: Object reference not set to an instance of an object.
12:30:36:070       at MyProjectName.Model.NameMatchViewModel.<.ctor>b__98_0(Object obj) in E:\My Projects\MAUI\MyProjectName-app-maui\MyProjectName\Model\NameMatchViewModel.cs:line 390

How can I fetch the selected item value?


Solution

  • The Parameter of SelectionChangedCommand will always be null since, you haven't defined SelectionChangedCommandParameter

    Either you can set SelectionChangedCommandParameter to SelectedItem or, Since you already binded your SelectedItem to LastImageTappedItem you can directly use it to set the ImageBGColor color property of selected item

    Option 1: Set SelectionChangedCommandParameter

    <CollectionView x:Name="myCollectionView"
        Margin="0,10,0,0"
        BackgroundColor="Transparent"
        IsVisible="{Binding Visibility}"
        ItemsSource="{Binding ImageItems}"
        ItemsLayout="VerticalGrid, 2"
        SelectionMode="Single"
        SelectionChangedCommand="{Binding ImageItemTappedCommand}"
        SelectedItem="{Binding LastImageTappedItem, Mode=TwoWay}"
        SelectionChangedCommandParameter="{Binding Source={x:Reference myCollectionView},Path=SelectedItem}"
    ...
    

    Option 2: Use LastImageTappedItem

    LastImageTappedItem.ImageBGColor = Color.FromArgb("#0091da");