We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to remove cards from card view collection in Xamarin.Forms

Platform: Xamarin.Forms |
Control: SfCardLayout

The SfCardView is a Syncfusion UI component that helps organizing the content in UI views as cards. This section explains how to remove a card from Xamarin.Forms CardView collection. You can remove the cards by enabling the SwipeToDismiss property in CardView.

 

Consider, the use case of showing a list of social media applications in a Xamarin.Forms application as follows.

 

Card gif

Creating the previous UI

 

You can achieve the previous UI using the following code snippets:

 

 

[XAML]

..
 
      <ListView   x:Name="EventListView"
                  RowHeight="100"
                  SeparatorVisibility="None"
                  ItemsSource="{Binding Items}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <cards:SfCardView Dismissed="SfCardView_Dismissed" SwipeToDismiss="True"  Margin="10">
                            <Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                                <StackLayout Orientation="Horizontal" >
                                    <Image Margin="10,0,0,0" HeightRequest="40" WidthRequest="40"
                                                  Source="{Binding Image}"/>
                                    <Label FontAttributes="Bold" Margin="10,0,0,0"
                                           FontSize="16"
                                           MaxLines="1"
                                           Text="{Binding Title}"
                                           LineBreakMode="NoWrap"
                                           TextColor="Black"
                                           HorizontalOptions="Start"
                                           VerticalOptions="Center" />
                                </StackLayout>
                            </Grid>
                        </cards:SfCardView>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

 

 [C#]

public class CardModel
{
    public string Title {get; set;}
 
    public string Image {get; set;}
}

 

 [C#]

public class CardViewModel
{
        public ObservableCollection<CardModel> Items { get; set; }
        public CardViewModel()
        {
            Items = new ObservableCollection<CardModel>()
            {
                new CardModel(){ Title = "Facebook" , Image = "FacebookFill.png"},
                new CardModel(){ Title = "Gmail" , Image = "GmailFill.png"},
                new CardModel(){ Title = "Instagram" , Image = "InstagramFill.png"},
                new CardModel(){ Title = "WhatsApp" , Image = "WhatsappFill.png"},
            };
        }
}

 

 

[C#]

public MainPage()
{
    InitializeComponent();
    items = (BindingContext as CardViewModel)?.Items;
}

 

Since, we have added that CardView in a ListView, even though it is been removed from CardView collection still exists in the ListView. So, to remove it thoroughly, we have removed that model in the Dismissed event as shown in the following code snippet.

 

[C#]

 

private void SfCardView_Dismissed(object sender, DismissedEventArgs e)

{

            var item = (sender as SfCardView)?.BindingContext as CardModel;

            if (items != null && item != null && items.Contains(item))

            {

                items.Remove(item);

            }

 }

 

 

You can get the complete sample from this GitHub location.

 

See Also

 

How to dismiss the cards programmatically

Available customization in cards

Notify events to visible index changed

 

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile