Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jessiezh0320 opened this issue Aug 21, 2022 · 14 comments
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView p/1 Work that is critical for the release, but we could probably ship without platform/android 🤖 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Milestone

Comments

@jessiezh0320
Copy link

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

@jessiezh0320 jessiezh0320 added the t/bug Something isn't working label Aug 21, 2022
@jessiezh0320 jessiezh0320 changed the title CollectionView's SelectionChanged method cannot triggered when tapping the item directly CollectionView's SelectionChanged method cannot be triggered when tapping the item directly Aug 21, 2022
@jsuarezruiz jsuarezruiz added area-controls-collectionview CollectionView, CarouselView, IndicatorView platform/android 🤖 labels Aug 22, 2022
@mattleibow mattleibow added this to the Backlog milestone Aug 22, 2022
@ghost
Copy link

ghost commented Aug 22, 2022

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@BaalaSrinivas
Copy link

@mattleibow : I am also facing similar issue when using Frame inside CollectionView, Is there any workaround which I can use until the issue is fixed?

@JuQiang
Copy link

JuQiang commented Sep 13, 2022

Maybe you can set the collectionview's SelectedItem as null in some event, thus the SelectionChanged event can be triggered when u tap it. After something happened when u tapped the row, for example u navigate to a page. when u back to this page again, the SelectedItems can be set null in event Appearing.
Here's my code in a navigation page:

private void MainPage_Appearing(object sender, EventArgs e)
{
    this.profileList.SelectedItem = null;// Won't trigger the SelectionChanged event if u always tapped the same line
}

protected void Profile_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (e.CurrentSelection.Count == 0) return; // avoid push profile page twice
    
    this.Navigation.PushAsync(new ProfilePage(e.CurrentSelection[0] as global::D3MAUI.Entity.Profile));
}

@JuQiang
Copy link

JuQiang commented Sep 13, 2022

@mattleibow : I am also facing similar issue when using Frame inside CollectionView, Is there any workaround which I can use until the issue is fixed?

maybe u can refer to my solution.

@PhoenixWyllow
Copy link

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.

@alfarajali
Copy link

I replaced frame with border, and it worked fine.

@marcelbeeker
Copy link

I'm facing with the same problem. The SelectionChanged event will not be triggered when tapping on a menu item. None of the solutions above worked for me. Can you tell when this bug will be fixed? Thanks in advance

@alfarajali
Copy link

I'm facing with the same problem. The SelectionChanged event will not be triggered when tapping on a menu item. None of the solutions above worked for me. Can you tell when this bug will be fixed? Thanks in advance

Would you please share a sample code for mimicking the issue?

@SamuelJames101
Copy link

This is still an issue

@georgetelinoiu
Copy link

Still an issue, was able to use a border instead of a frame to get similar results.

@Micwi
Copy link

Micwi commented Nov 22, 2023

Still an issue, used border for now. Hope to see this fixed.

@Zhanglirong-Winnie Zhanglirong-Winnie added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Nov 30, 2023
@Zhanglirong-Winnie
Copy link
Collaborator

Verified this issue with Visual Studio Enterprise 17.9.0 Preview 1.0. Can repro on android platform.

@samhouts samhouts added the p/1 Work that is critical for the release, but we could probably ship without label Dec 7, 2023
@andersondamasio
Copy link

Same problem here.
Visual Studio version: 17.9.4

@prasad210
Copy link

prasad210 commented May 14, 2024

Any update on this open issue as i am unable to get a work around to make the selection change trigger the command handler. Below is the CollectionView Code
`
<CollectionView.ItemsLayout>

</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>

                    <Grid HorizontalOptions="Start" Padding="10">
                            <ccontrols:TrolleyProgress 
                            HorizontalOptions="StartAndExpand" 
                            VerticalOptions="Start" 
                            CleaningProgress="{Binding CleanProgress}"
                            TrolleyStatus="{Binding RunProgressStatus}"
                            TrolleyName="{Binding ., Converter={StaticResource TrolleyProgressNameConverter}}"
                            Percentage="{Binding RunProgressPercentage}"
                            Total="{Binding AllocationCount}"
                            Completed="{Binding DistinctCompletedCleanCount}"
                            Behind="{Binding CompletedBehindCount}"
                            ShowMoveArrow="{Binding BindingContext.IsEndOfShift, Source={x:Reference trolleyList}}"
                            ScrollableDailyProgress="False" />
                        </Grid>

                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>`

For some reason the pasted code removes some of the code hence pasting the screen shot
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView p/1 Work that is critical for the release, but we could probably ship without platform/android 🤖 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests