MAUI collection view selection Mode multiple not working

Bhuwan 881 Reputation points
2023-04-20T03:27:30.2666667+00:00

Hi I am trying to use collection view selection mode multiple and selection change event but it's not working and selection change event not fire

<CollectionView x:Name="CVStudentData" HorizontalScrollBarVisibility="Never" VerticalScrollBarVisibility="Never" SelectionMode="Multiple"
                        Margin="5,10,5,0" BackgroundColor="White" VerticalOptions="CenterAndExpand" HorizontalOptions="FillAndExpand"
                        SelectionChanged="CVStudentData_SelectionChanged">
            
            <CollectionView.Header>
                <Grid HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand">
                    <Frame IsVisible="False" CornerRadius="10" HasShadow="False" IsClippedToBounds="True" BorderColor="Black">
                        <Label Text="No record found" TextColor="Red" FontAttributes="Bold" FontSize="16" VerticalOptions="CenterAndExpand" 
                               HorizontalOptions="CenterAndExpand"/>
                    </Frame>
                </Grid>
            </CollectionView.Header>
            <CollectionView.ItemsLayout>
                <GridItemsLayout Orientation="Vertical" Span="1" VerticalItemSpacing="5" HorizontalItemSpacing="10"/>
            </CollectionView.ItemsLayout>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Frame CornerRadius="10" Padding="10" Margin="0" HasShadow="False" IsClippedToBounds="True" BorderColor="Black" 
                                   BackgroundColor="Black">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="Selected">
                                    <VisualState.Setters>
                                        <Setter Property="BackgroundColor" Value="Red" />
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid RowSpacing="10" ColumnDefinitions="*,*">
                            <Label Grid.Column="0" Text="{Binding Id}" TextColor="White" FontSize="14"
                                                VerticalOptions="CenterAndExpand" HorizontalOptions="StartAndExpand"/>
                            <Label Grid.Column="1" Text="{Binding StudentName}" TextColor="White" FontSize="14"
                                                VerticalOptions="CenterAndExpand" HorizontalOptions="StartAndExpand"/>
                        </Grid>
                    </Frame>
                </DataTemplate>
            </CollectionView.ItemTemplate>
            <CollectionView.Footer>
                <StackLayout Orientation="Vertical" Padding="0,5,0,0">
                    <Label Text=""/>
                </StackLayout>
            </CollectionView.Footer>
        </CollectionView>

 List<StudentModel> studentModels = new List<StudentModel>(); 
studentModels = new List<StudentModel>();
        studentModels.Add(new StudentModel { Id = 1, StudentName = "Sanajay" });
        studentModels.Add(new StudentModel { Id = 2, StudentName = "Ajay" });
        studentModels.Add(new StudentModel { Id = 3, StudentName = "Rajesh" });
        studentModels.Add(new StudentModel { Id = 4, StudentName = "Prakash" });
        CVStudentData.ItemsSource = studentModels;

    private void CVStudentData_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        try
        {

        }
        catch (Exception)
        {
        }
    }
Developer technologies | .NET | .NET MAUI
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,436 Reputation points Microsoft External Staff
    2023-04-20T09:06:06.4133333+00:00

    Hello, You set Frame as DataTemplate for the CollectionView, please replace the Frame with Border, refer to the following code:

    <Border  StrokeShape="RoundRectangle 10,10,10,10" Padding="10" Margin="0"  Stroke="Black"
                                       BackgroundColor="Black">
    

    And there is a known issue reported at GitHub- CollectionView does not select an element with a Frame as DataTemplate #8416, you could follow the progress.

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.