I have a collection view with about 25 elements, I want to allow user to "star" these elements in order to display them in a special collection view in another page. There is a dedicated button to star the element.
I have no idea about how to do this. Any help?
I am working with Xamarin.Forms.

Amira BedhiafiRank #601 · 4K reputationPosted Mar 18, 2025, 5:59 PM EDT
Create a data model that includes a property to track whether an item is "starred" or not.
public class Item { public string Name { get; set; } public bool IsStarred { get; set; } }In your XAML, define a
CollectionViewand bind it to a list of items. Include aButtonin theDataTemplateto allow users to "star" an item.Create a
ViewModelto manage the list of items and handle the "star" command.public class ItemsViewModel : INotifyPropertyChanged { public ObservableCollection Items { get; set; } = new ObservableCollection(); public Command StarCommand { get; } public ItemsViewModel() { // Initialize the list with some items for (int i = 1; i (StarItem); } private void StarItem(Item item) { item.IsStarred = !item.IsStarred; // Toggle the star status OnPropertyChanged(nameof(Items)); // Notify the UI of changes } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); }Set the
BindingContextof your page to theViewModel.public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); BindingContext = new ItemsViewModel(); } }Create a new page to display the starred items. Use another
CollectionViewand bind it to a filtered list of starred items.Add a method to get starred items in your
ViewModel.public List GetStarredItems() => Items.Where(item => item.IsStarred).ToList();Create a new page to display starred items.
Add a button in your main page to navigate to the starred items page.
private async void OnViewStarredItemsClicked(object sender, EventArgs e) { var viewModel = BindingContext as ItemsViewModel; await Navigation.PushAsync(new StarredItemsPage(viewModel)); }