question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CollectionView's SelectionChanged method cannot be triggered when tapping the item directly

See original GitHub issue

Description

CollectionView’s SelectionChanged method cannot triggered when tapping the item directly.

Steps to Reproduce

1.create a maui app;

2.copy the following code to the MainPage.xaml:

     <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiCollectionApp.MainPage"
              xmlns:local="clr-namespace:MauiCollectionApp"
             x:Name="myPage"
             >

    <ContentPage.BindingContext>
        <local:MyViewModel></local:MyViewModel>
    </ContentPage.BindingContext>

   <CollectionView  ItemsSource="{ Binding Data}" x:Name="mCollectionView" 
                    SelectionChanged="mCollectionView_SelectionChanged"
                    SelectionMode="Single"
                    >
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                <Grid Padding="5" RowSpacing="0" BackgroundColor="Yellow">
                    <Frame CornerRadius="40" BorderColor="Gray"  BackgroundColor="Green">
                        <HorizontalStackLayout  Margin="3" >
                            <Label Text="{Binding Name}" BackgroundColor="Gray"/>
                            <Label Text="{Binding Car.Make}" Margin="5,0,5,0" />

                            <Button Text="delete"  Margin="10,0,0,0"
                                BackgroundColor="Red"
                                Command="{Binding  Path= BindingContext.RemoveEquipmentCommand,Source={Reference mCollectionView }}"  CommandParameter="{Binding .}"  
                                 />
                        </HorizontalStackLayout>
                    </Frame>
                   </Grid>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
   </CollectionView>

</ContentPage>

MyViewModel.cs

public class MyViewModel: INotifyPropertyChanged 
    {
        public  ObservableCollection<MyModel> Data { get; set; }


        public ICommand RemoveEquipmentCommand => new Command<MyModel>(ReMoveItem);

        private void ReMoveItem(MyModel obj)
        {
            System.Diagnostics.Debug.WriteLine(" the selected item's name  is:  " + obj.Name   );

            Data.Remove(obj);
        }

        public MyViewModel() {
            Data = new ObservableCollection<MyModel>();

            Data.Add(new MyModel { Name ="model_1", Car= new Vehicle {Make="Make1" } });
            Data.Add(new MyModel { Name = "model_2", Car = new Vehicle { Make = "Make2" } });
            Data.Add(new MyModel { Name = "model_3", Car = new Vehicle { Make = "Make3" } });
            Data.Add(new MyModel { Name = "model_4", Car = new Vehicle { Make = "Make4" } });


        }

        bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (Object.Equals(storage, value))
                return false;

            storage = value;
            OnPropertyChanged(propertyName);
            return true;
        }

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

3.Deploy the app to a android device, and tap directly on the item, the SelectionChanged method is not being triggered and will be triggered only if clicking on the frame border or outside of it.

Version with bug

Unknown/Other (please specify)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

android 10

Did you find any workaround?

No response

Relevant log output

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:8

github_iconTop GitHub Comments

2reactions
alfarajalicommented, Sep 23, 2022

I replaced frame with border, and it worked fine.

2reactions
PhoenixWyllowcommented, Sep 13, 2022

I had a similar issue with a set of RadioButtons as well as a CollectionView. In one case I made it work with a simple ContentTemplate instead of using the Frame directly

<ControlTemplate x:Key="Framed">
    <Frame>
        <ContentPresenter />
    </Frame>
</ControlTemplate>

In another case I replaced the Frame with a Border since it made no difference. This of course assumes you need the Frame in the first place - removing it also works fine in my uses.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CollectionView SelectionChanged method not triggering ...
I'm creating a CollectionView in .net MAUI where i'm using Frame control inside the data template. As a result of that, when I...
Read more >
SelectionChanged event not getting invoked on Android?
On Android, I'm having a problem. I can click and drag to reorder rows but the SelectionChanged event appears to not get invoked....
Read more >
Configure CollectionView item selection - .NET MAUI
CollectionView defines a SelectionChanged event that is fired when the SelectedItem property changes, either due to the user selecting an item ...
Read more >
How To Work With CollectionView in Xamarin Forms
CollectionView defines a SelectionChanged event that is fired when the SelectedItem property changes, either due to the user selecting an item ...
Read more >
CollectionView's SelectionChanged ...
When I tap, hold, and then drag the cell within the CollectionView, only then is SelectionChangedCommand Binding invoked. Target Devices. This is only...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found