Skip to content

CollectionView SelectionChanged not working when item is in Frame #15016

Description

@janseris

Description

SelectionChanged command is not called when Frame is in CollectionView item template.
MAUI 7.0.81

<CollectionView ItemsSource="{Binding DiscoveredBeacons}" Margin="10"
                        SelectionMode="Single" SelectedItem="{Binding Selected}" SelectionChangedCommand="{Binding SelectedItemChangedCommand}"
                        >
            <CollectionView.ItemsLayout>
                <LinearItemsLayout ItemSpacing="5" Orientation="Vertical" />
            </CollectionView.ItemsLayout>

            <CollectionView.ItemTemplate>
                <DataTemplate x:DataType="kontrolniBody:KontrolniBod">
                    <Frame BackgroundColor="{StaticResource Secondary}" 
                                   Padding="10, 5" CornerRadius="10" >

                        <Grid ColumnDefinitions="*, *, *, *, *, *, *, *, *, *, *, *" RowDefinitions="Auto">

                            <!-- Row 1 (ID, Nazev, MAC) -->
                            <Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="1" 
                                   VerticalTextAlignment="Center" HorizontalTextAlignment="Center"
                                   Text="{x:Static fonts:MaterialIconsFont.Identifier}" FontFamily="MaterialIcons" FontSize="Large" />

                            <Label Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="1" 
                                   VerticalTextAlignment="Center" HorizontalTextAlignment="Center"
                                   Text="{Binding ID}" />

                            <BoxView Grid.Row="0" Grid.Column="2" WidthRequest="1" HeightRequest="20" BackgroundColor="Gray" />

                            <Label Grid.Row="0" Grid.Column="3" Grid.ColumnSpan="3" 
                                   VerticalTextAlignment="Center" HorizontalTextAlignment="Start"
                                   Text="{Binding Nazev}" />

                            <BoxView Grid.Row="0" Grid.Column="6" WidthRequest="1" HeightRequest="20" BackgroundColor="Gray" />

                            <Label Grid.Row="0" Grid.Column="7" Grid.ColumnSpan="5" VerticalTextAlignment="Center"
                                   Text="{Binding Identifikator}" />
                        </Grid>
                    </Frame>

                </DataTemplate>
            </CollectionView.ItemTemplate>

            <CollectionView.EmptyView>
                Žádné záznamy k dispozici
                <!-- BUG: UI element content doesn't work on Android: https://github.com/dotnet/maui/issues/10819: -->
                <!-- <Label Text="No items" />-->
            </CollectionView.EmptyView>
        </CollectionView>

ViewModel:

partial class KontrolniBodyViewModel
    {
        /// <summary>The backing field for <see cref="SelectedItemChangedCommand"/>.</summary>
        [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.RelayCommandGenerator", "8.1.0.0")]
        private global::CommunityToolkit.Mvvm.Input.AsyncRelayCommand? selectedItemChangedCommand;
        /// <summary>Gets an <see cref="global::CommunityToolkit.Mvvm.Input.IAsyncRelayCommand"/> instance wrapping <see cref="SelectedItemChanged"/>.</summary>
        [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.RelayCommandGenerator", "8.1.0.0")]
        [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
        public global::CommunityToolkit.Mvvm.Input.IAsyncRelayCommand SelectedItemChangedCommand => selectedItemChangedCommand ??= new global::CommunityToolkit.Mvvm.Input.AsyncRelayCommand(new global::System.Func<global::System.Threading.Tasks.Task>(SelectedItemChanged));
    }
 [RelayCommand]
        async Task SelectedItemChanged()
        {
            if (Selected is null)
            {
                return;
            }
            if (SendingResultToDB)
            {
                return;
            }
            int beaconID = Selected.ID;
            //unselect for next page visit (makes user stuck when there is only 1 item)
            Selected = null;
            ... etc
        }

When you comment out the Frame, the command starts running (breakpoint is hit etc.) on selection changed.

Edit: I had to remove Frame in all places where I wanted SelectionChanged work in CollectionView item template.

Steps to Reproduce

Click collectionview with the item template

Link to public reproduction project repository

none

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android, I was not able test on other platforms

Affected platform versions

Android 12

Did you find any workaround?

No response

Relevant log output

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions