How to remove cards from card view collection in Xamarin.Forms CardLayout?
The Xamarin CardLaypout, 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.
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 thatCardView 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
Conclusion
I hope you enjoyed learning about how to remove cards from card view collection in Xamarin.Forms.
You can refer to our Xamarin.Forms CardLayout feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin.Forms CardLayout documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!