Share via

Item in CollectionView is not highlighted if used in SwipeView ?

Denis Creator 196 Reputation points
2021-11-18T20:28:32.53+00:00

Hi all,

I have the following CollectionView with SwipeView inside:

 <CollectionView
    SelectionMode="Single"
    ItemsSource="{Binding Source}">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <SwipeView>
                <SwipeView.RightItems>
                    ...
                </SwipeView.RightItems>
            </SwipeView>
            ...
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

But when I click on item in CollectionView is it clicked but not highlighted !!

If I remove SwipeView then selected item is highlighted !! How to fix it ??

I want to use SwipeView but see that element is selected ?!

Developer technologies | .NET | Xamarin

2 answers

Sort by: Most helpful
  1. vanKraster 201 Reputation points
    2026-03-30T13:18:58.2433333+00:00

    @Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.)

    Today in MAUI there is the same problem. I explain our problem, I have this layout:
    <DataTemplate>

    <SwipeView>

    <SwipeView.BotomItems>

    <Border> here inside is my layout </Border

    </SwipeView>
    </DataTemplate>

    If I put this code

      <VisualStateManager.VisualStateGroups>
          <VisualStateGroupList>
              <VisualStateGroup x:Name="CommonStates">
                  <VisualState x:Name="Selected">
                      <VisualState.Setters>
                          <Setter Property="BackgroundColor" Value="{DynamicResource primary}" />
                      </VisualState.Setters>
                  </VisualState>
                  <VisualState x:Name="Normal">
                      <VisualState.Setters>
                          <Setter Property="BackgroundColor" Value="{DynamicResource black025}" />
                      </VisualState.Setters>
                  </VisualState>
              </VisualStateGroup>
          </VisualStateGroupList>
      </VisualStateManager.VisualStateGroups>
    

    Before </Swipeview> It changes the backgorund underneath my layout aka border because it changes the background color of the SWIPE ! while I need to change the background color of the <Border> to see it visually in UI !

    If I move the visual state before </Border> it doesn't work.

    So I neet to change the visual state of my element in order that user have a visual feedback, otherwise the swipe is underneath my layout, visible only if I swipe.

    SOLUTION:

    Add

     <Border x:Name="RootBorder"
    

    Then add TargetName to redirect to the desired object.

       <VisualStateManager.VisualStateGroups>
           <VisualStateGroupList>
               <VisualStateGroup x:Name="CommonStates">
                   <VisualState x:Name="Selected">
                       <VisualState.Setters>
                           <Setter TargetName="RootBorder" Property="BackgroundColor" Value="Red" />
                       </VisualState.Setters>
                   </VisualState>
                   <VisualState x:Name="Normal">
                       <VisualState.Setters>
                           <Setter TargetName="RootBorder" Property="BackgroundColor" Value="White" />
                       </VisualState.Setters>
                   </VisualState>
               </VisualStateGroup>
           </VisualStateGroupList>
       </VisualStateManager.VisualStateGroups>
    
    0 comments No comments

  2. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,456 Reputation points Microsoft External Staff
    2021-11-19T08:09:44.83+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    I test it through the code snippet you provided, and only add the content of SwipeView and RightItems, when click CollectionView ,it is highlighted. It works in both iOS and Android, I can't reproduce this issue.

    <StackLayout>  
            <CollectionView  
                SelectionMode="Single"  
                ItemsSource="{Binding SourceList}">  
                <CollectionView.ItemTemplate >  
                    <DataTemplate>  
                        <SwipeView >  
                            <SwipeView.RightItems>  
                                <SwipeItems><!--add SwipeItem-->  
                                    <SwipeItem Text="delete" BackgroundColor="Blue" Invoked="SwipeItem_Invoked">  
                                    </SwipeItem>  
                                </SwipeItems>  
                            </SwipeView.RightItems>  
                            <StackLayout><!--add this content-->  
                                <Label Text="{Binding .}" HeightRequest="80"></Label>  
                            </StackLayout>  
                          
                        </SwipeView>  
                    </DataTemplate>  
                </CollectionView.ItemTemplate>  
            </CollectionView>  
              
        </StackLayout>  
    

    Then I try to set SwipeView background color to white, and add VisualStateManager.VisualStateGroups , it also works, you could have a try.

    <CollectionView  
                SelectionMode="Single"  
                ItemsSource="{Binding SourceList}">  
                <CollectionView.ItemTemplate >  
                    <DataTemplate>  
                        <SwipeView BackgroundColor="White">  
                            <SwipeView.RightItems>  
                                <SwipeItems>  
                                    <SwipeItem Text="delete" BackgroundColor="Blue" Invoked="SwipeItem_Invoked">  
                                    </SwipeItem>  
                                </SwipeItems>  
                            </SwipeView.RightItems>  
                            <StackLayout>  
                                <Label Text="{Binding .}" HeightRequest="80"></Label>  
                            </StackLayout>  
                            <VisualStateManager.VisualStateGroups>  
                                <VisualStateGroup Name="CommonStates">  
                                    <VisualState Name="Normal" />  
                                    <VisualState Name="Selected">  
                                        <VisualState.Setters>  
                                            <Setter Property="BackgroundColor" Value="Yellow" />  
                                        </VisualState.Setters>  
                                    </VisualState>  
                                </VisualStateGroup>  
                            </VisualStateManager.VisualStateGroups>  
                        </SwipeView>  
                    </DataTemplate>  
                </CollectionView.ItemTemplate>  
            </CollectionView>  
    

    I'm afraid you could share more operation information, running environment and code snippets or share me a demo to test.
    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 comments No comments

Your answer

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