xamarin forms expand and collapse comments on button click

Chirag Pokiya 96 Reputation points
2021-09-22T08:23:47.87+00:00

how to create expand view in Xamarin form when I click on Comment button like below screen shot?
134218-screenshot-2021-09-22-at-14856-pm.png

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,951 Reputation points
    2021-09-23T01:46:16.973+00:00

    Hello @Chirag Pokiya ,​

    Welcome to our Microsoft Q&A platform!

    To achieve the function as the pictures shown, try using Community.Toolkit.Expander control which provides an expandable container to host any content. Here is the related doc, you could refer to it: https://learn.microsoft.com/en-us/xamarin/community-toolkit/views/expander#create-expander-content-on-demand

    However, the expandable container should only be expanded when clicking the comment menu in the picture. So we need to make a little change, try to remove the Expander.Header template and use the button/menu to change the expanding status instead. Here is the sample code, you could refer to it:

       <ContentPage ...  
               xmlns:xct="http://xamarin.com/schemas/2020/toolkit">  
         
           <ContentPage.Content>  
               <StackLayout>  
                   <Button Text="button" Clicked="Button_Clicked" />  
                   <xct:Expander x:Name="expander" BackgroundColor="LightBlue">  
                       <xct:Expander.ContentTemplate>  
                           <DataTemplate>  
                               <Grid Padding="10">  
                                   <Grid.ColumnDefinitions>  
                                       <ColumnDefinition Width="Auto" />  
                                       <ColumnDefinition Width="Auto" />  
                                   </Grid.ColumnDefinitions>  
                                   <Image Source="yali"  
                                          Aspect="AspectFill"  
                                          HeightRequest="120"  
                                          WidthRequest="120" />  
                                   <Label Grid.Column="1"  
                                          Text="Details"  
                                          FontAttributes="Italic" />  
                               </Grid>  
                           </DataTemplate>  
                       </xct:Expander.ContentTemplate>  
                   </xct:Expander>  
               </StackLayout>  
           </ContentPage.Content>  
       </ContentPage>  
         
       //button click event  
       private async void Button_Clicked(object sender, EventArgs e)  
       {  
           expander.IsExpanded = !expander.IsExpanded;  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    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.