Interactions
This page describes how to handle user interaction of the CollectionView
Context menus
Use the SwipeView to define custom commands for each item. Below it's shown an example:
class MainPageSwipeState
{
public ObservableCollection<Person> Persons { get; set; }
}
class MainPageSwipe : Component<MainPageSwipeState>
{
protected override void OnMounted()
{
var person1 = new Person("John", "Doe", new DateTime(1980, 5, 10));
var person2 = new Person("Jane", "Smith", new DateTime(1990, 6, 20));
var person3 = new Person("Alice", "Johnson", new DateTime(1985, 7, 30));
var person4 = new Person("Bob", "Williams", new DateTime(2000, 8, 15));
var person5 = new Person("Charlie", "Brown", new DateTime(1995, 9, 25));
State.Persons = new ObservableCollection<Person>(new[] { person1, person2, person3, person4, person5 });
base.OnMounted();
}
public override VisualNode Render()
{
return ContentPage(
CollectionView()
.ItemsSource(State.Persons, RenderPerson)
);
}
private VisualNode RenderPerson(Person person)
{
return SwipeView(
VStack(spacing: 5,
Label($"{person.FirstName} {person.LastName}"),
Label(person.DateOfBirth.ToShortDateString())
.FontSize(12)
)
.VCenter()
)
.LeftItems(new SwipeItems
{
new SwipeItem()
.IconImageSource("archive.png")
.Text("Archive")
.BackgroundColor(Colors.Green),
new SwipeItem()
.IconImageSource("delete.png")
.Text("Delete")
.BackgroundColor(Colors.Red)
.OnInvoked(()=>State.Persons.Remove(person))
})
.HeightRequest(60);
}
}
Pull to refresh
The "pull to refresh" feature allows you to execute a command when the user pulls down the collection view control to refresh the list.
For example:

Load data incrementally
The below example shows how you can load data incrementally as the user scrolls down the list:
You may be tempted to use code like SetState(s => s.Persons.Add(person)) but, in this specific case, it's not required: the CollectionView is bound to the Persons ObservableCollection and it's automatically refreshed at every change of the collection without requiring the component refresh.
Please note that, in all other cases (i.e. excluding ObservableCollection properties) you must update the state using the SetState() method when you need to update the view.
Last updated
Was this helpful?