Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Select-Deselect not working in Xamarin iOS using CollectionView

ukku2215ukku2215 Member ✭✭
edited December 2020 in Xamarin.Forms

I have a CollectionView in which user can select or deselect multiple names from the ItemSource provided. It is working perfect on Android i.e user can select-deselect multiple names. But in iOS what happens is, if user selects one name and tries to select another name it is not getting selected. But if I scroll a bit and try to select or deselect the name it works. In short when a view is scrolled even if little bit it works fine. User should be able to select or deselect the names without scrolling the view.

Answers

  • YelinzhYelinzh Member, Xamarin Team Xamurai

    But in iOS what happens is, if user selects one name and tries to select another name it is not getting selected.

    Hi, ukku2215. I tested a basic demo to test the collectionView, the multi selection works fine on both Android and iOS. What the version of Xamarin.Forms and XCode did you test? Try to update them to the lastest stable version and test again.

    If the doesn't work, please post the related code about page and some details about the test environment. It'll help to reproduce the issue to get a solution.


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • ukku2215ukku2215 Member ✭✭
    edited December 2020

    @Yelinzh am on
    Xamarin forms version (4.8.0.1364)
    xcode version 12.0(12A7209)

    Updating to latest versions did not resolve my issue.

    here is my XAML,

    <CollectionView  x:Name="NameOfCollectionView"
                     Grid.Row="2"
                     Grid.ColumnSpan="2"
                     HeightRequest="200"
                     ItemsSource="{Binding MyList, Mode=TwoWay}"
                     SelectionMode="Multiple">
                     <CollectionView.ItemTemplate>
                      <DataTemplate>
                       <Grid Padding="10">
                        <Grid.RowDefinitions>
                         <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                          <VisualStateManager.VisualStateGroups>
                           <VisualStateGroup Name="CommonStates">
                            <VisualState Name="Normal">
                             <VisualState.Setters>
                              <Setter TargetName="FullName"
                                      Property="Label.TextColor"
                                      Value="Black" />
                             </VisualState.Setters>
                            </VisualState>
                            <VisualState Name="Selected">
                             <VisualState.Setters>
                              <Setter Property="BackgroundColor"
                                      Value="#6DB244" />
                              <Setter TargetName="FullName"
                                      Property="Label.TextColor"
                                      Value="White" />
                              </VisualState.Setters>
                             </VisualState>
                            </VisualStateGroup>
                           </VisualStateManager.VisualStateGroups>
                            <Label Grid.Row="0"
                                   FontSize="16"
                                   x:Name="FullName"
                                   Text="{Binding FullName}"
                                   HorizontalOptions="CenterAndExpand"
                                   VerticalOptions="CenterAndExpand" FontAttributes="Bold">
                            </Label>
                           </Grid>
                          </DataTemplate>
                         </CollectionView.ItemTemplate>
                        <CollectionView.Behaviors>
                         <behaviors:EventToCommandBehavior EventName="SelectionChanged"
                                                           Command="{Binding CollectionViewSelectionChangedCommand}"
                                                           CommandParameter="{x:Reference Page}" />
     </CollectionView.Behaviors>
    </CollectionView>
    
  • YelinzhYelinzh Member, Xamarin Team Xamurai

    Hi, ukku2215. I tested the posted code on my side, it still works well. Because there is no the code about the 'Behavior' class, I removed the <CollectionView.Behaviors > for test.

    Try to delete the behavior of the collection to test if the project will work.


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • ukku2215ukku2215 Member ✭✭

    @Yelinzh can you post you code that you used after removing the behavior. or you simply removed it and it worked?
    but that's where my code to select and deselect resides.

  • YelinzhYelinzh Member, Xamarin Team Xamurai

    Here is the related code, you could test it on your side.

    <ContentPage ...>
        <ContentPage.Content>
            <Grid >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Image Source="grid_" Grid.Row="1"/>
                <StackLayout Grid.Row="2"  Grid.ColumnSpan="2">
                    <Label Text="testing..." />
                </StackLayout>
    
                <CollectionView  x:Name="NameOfCollectionView"
                     Grid.Row="0"
                     Grid.ColumnSpan="2"
                     HeightRequest="200"
                     SelectionMode="Multiple">
                    <CollectionView.ItemsSource>
                        <x:Array Type="{x:Type x:String}">
                            <x:String>mono</x:String>
                            <x:String>monodroid</x:String>
                            <x:String>monotouch</x:String>
                            <x:String>monorail</x:String>
                            <x:String>monodevelop</x:String>
                            <x:String>monotone</x:String>
                            <x:String>monopoly</x:String>
                            <x:String>monomodal</x:String>
                            <x:String>mononucleosis</x:String>
                            <x:String>mononucleosis2</x:String>
                        </x:Array>
                    </CollectionView.ItemsSource>
                    <CollectionView.ItemTemplate>
                        <DataTemplate>
                            <Grid Padding="10">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup Name="CommonStates">
                                        <VisualState Name="Normal">
                                            <VisualState.Setters>
                                                <Setter TargetName="FullName" Property="Label.TextColor" Value="Black" />
                                            </VisualState.Setters>
                                        </VisualState>
                                        <VisualState Name="Selected">
                                            <VisualState.Setters>
                                                <Setter Property="BackgroundColor" Value="#6DB244" />
                                                <Setter TargetName="FullName" Property="Label.TextColor" Value="White" />
                                            </VisualState.Setters>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <Label Grid.Row="0"
                                   FontSize="16"
                                   Text="{Binding }"
                                   HorizontalOptions="CenterAndExpand"
                                   VerticalOptions="CenterAndExpand" FontAttributes="Bold">
                                </Label>
                            </Grid>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>
            </Grid>
        </ContentPage.Content>
    </ContentPage>
    

    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

Sign In or Register to comment.