)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to render a custom content to a Carousel

Platform: Xamarin.Forms |
Control: SfCarousel |
Published Date: March 1, 2017 |
Last Revised Date: June 24, 2019

We can render the custom view by using “ItemTemplate” property in Carousel. The following steps will help to render the custom view.

 

Step1: Add needed assemblies to have the carousel control

Step2: Create a model view which holds the data (CarouselModel)

Step 3: ItemTemplate property of carousel is used to customize the contents of carousel items. So, assign the collection of your template view to carousel datasource.

 

The below code illustrates the way to render a custom content to carousel

Code snippet:

public partial class KBSolutionPage : ContentPage
{
public KBSolutionPage()
{
    InitializeComponent();
 
    //Initialize the carousel control
 
    SfCarousel sfCarousel = new SfCarousel();
 
    if (Device.OS == TargetPlatform.Android)
    {
        sfCarousel.ItemWidth = 170;
        sfCarousel.ItemHeight = 250;
    }
    if (Device.OS == TargetPlatform.iOS)
    {
        sfCarousel.ItemWidth = 150;
        sfCarousel.ItemHeight = 300;
    }
 
    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 = Color.Green, HeightRequest = 200, WidthRequest = 200 };
        buttonText.SetBinding(Button.TextProperty, "ButtonString");
        var labelText = new Label() { TextColor = Color.Red, HorizontalOptions = LayoutOptions.Center };
        labelText.SetBinding(Label.TextProperty, "LabelString");
 
        stack.Children.Add(buttonText);
        stack.Children.Add(labelText);
 
        return stack;
 
    });
    sfCarousel.ItemTemplate = carouselDataTemplate;
    sfCarousel.DataSource = carouselModel;
    this.Content = sfCarousel;
}
}
 
public class CarouselModel
{
public CarouselModel(string buttonstr, string labelstr)
{
    ButtonString = buttonstr;
    LabelString = labelstr;
}
private string buttonString;
public string ButtonString
{
    get { return buttonString; }
    set { buttonString = value; }
}
private string labelString;
public string LabelString
{
    get { return labelString; }
    set { labelString = value; }
}
} 
}

 

Output:

Custo, view in SfCarousel

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon