How to perform an operation while changing the CarouselItem?

21 Apr 20251 minute to read

We can perform the operation while changing the CarouselItem using the SelectionChanged event. The SelectionChanged event returns the OldItem and selected NewItem.

<carousel:SfCarousel x:Name="carousel"
                     ItemsSource="{Binding ImageCollection}"
                     ItemTemplate="{StaticResource itemTemplate}" 
                     ItemHeight="170"
                     ItemWidth="270"
                     SelectionChanged="Carousel_SelectionChanged"/>
SfCarousel carousel = new SfCarousel()
{
    ItemHeight = 170,
    ItemWidth = 270,
};

carousel.ItemTemplate = itemTemplate;
carousel.SetBinding(SfCarousel.ItemsSourceProperty, "ImageCollection");
carousel.SelectionChanged += Carousel_SelectionChanged;

// Trigger when selection changed in the carousel item.
private void Carousel_SelectionChanged(object sender, Syncfusion.Maui.Core.Carousel.SelectionChangedEventArgs e)
{
    int count = (sender as SfCarousel).SelectedIndex + 1;
    DisplayAlert("SelectionChanged", "Carousel item:" + count + " has Selected", "Ok");
}