Articles in this section
Category / Section

How to render a custom content to .NET MAUI Carousel?

2 mins read

This section demonstrates how to render custom content in a .NET MAUI Carousel. Custom views can be rendered by utilizing the ItemTemplate property of the Carousel. This property allows customization of the contents of carousel items. Follow these steps to render custom views:

C#
Model

public class CarouselModel 
{
    public CarouselModel(string buttonstr, string labelstr)
    {
        ButtonString = buttonstr;
        LabelString = labelstr;
    }
    public string ButtonString { get; set; }
    public string LabelString { get; set; }
}

C#

 public MainPage() 
 {
     InitializeComponent();

     SfCarousel sfCarousel = new SfCarousel();            

     var carouselModel = new List<CarouselModel> 
     {
     new CarouselModel ("Button1","Label1"),
     new CarouselModel ("Button2","Label2"),
     new CarouselModel ("Button3","Label3"),
     new CarouselModel ("Button4","Label4"),
     new CarouselModel ("Button5","Label5")
     };
     var carouselDataTemplate = new DataTemplate(() =>
     {

         var stack = new StackLayout();
         var buttonText = new Button() { TextColor = Colors.DarkGreen, FontSize =20, BackgroundColor = Color.FromArgb("#a9afb0"), HeightRequest = 200, WidthRequest = 200 };
         buttonText.SetBinding(Button.TextProperty, "ButtonString");
         var labelText = new Label() { TextColor = Colors.Red, FontSize = 20, HorizontalOptions = LayoutOptions.Center };
         labelText.SetBinding(Label.TextProperty, "LabelString");

         stack.Children.Add(buttonText);
         stack.Children.Add(labelText);

         return stack;

     });
     sfCarousel.ItemTemplate = carouselDataTemplate;
     sfCarousel.ItemsSource = carouselModel;
     this.Content = sfCarousel;
 }

Output

AndroidEmulator-pixel_5_-_api_34_55542024-03-1515-54-39-ezgif.com-video-to-gif-converter.gif

Download the complete sample from GitHub

Conclusion
I hope you enjoyed learning how to render custom content to .NET MAUI Carousel.

You can refer to our .NET MAUI Carousel feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI Carousel documentation to understand how to present and manipulate data.

For current customers, check out our .NET MAUI components on the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to explore our .NET MAUI Navigation Drawer and other .NET MAUI components.

Please let us know in the following comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac, or feedback portal, or the feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied