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. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Conditional Formatting

Thread ID:





146192 Jul 24,2019 11:24 PM UTC Jul 25,2019 01:31 PM UTC Xamarin.Forms 1
Tags: SfCardView
Asked On July 24, 2019 11:24 PM UTC

Can you show me how to conditionally change the background color of a card based on certain criteria?

For examble, a databound card layout that reads names. If the name is male I want the card to be blue. If it is female, I want the card to be pink.


Rachel A [Syncfusion]
Replied On July 25, 2019 01:31 PM UTC

Hi Michael, 
We can achieve your requirement by changing the item template of the SfCardView based on the Rating property of the model using DataTemplateSelector. Please check the below code snippet and sample for your reference. 
public class CustomDataTemplateSelector : DataTemplateSelector
        public DataTemplate BlueBackgroundTemplate { getset; }
        public DataTemplate PinkBackgroundTemplate { getset; }

        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
            if (Convert.ToDouble(((CardData)item).Rating) <= 3)
                ((CardData)item).BlueBackgroundColor = Color.Blue;
                return BlueBackgroundTemplate;
                ((CardData)item).PinkBackgroundColor = Color.Pink;
                return PinkBackgroundTemplate;

            <DataTemplate x:Key="blueTemplate">
                <card:SfCardView BackgroundColor="{Binding BlueBackgroundColor}">
                    <local: CustomStackLayout />

            <DataTemplate x:Key="pinkTemplate">
                 <card:SfCardView BackgroundColor="{Binding PinkBackgroundColor}">
                    <local:CustomStackLayout />

            <local:CustomDataTemplateSelector x:Key="dataTemplateSelector"
                BlueBackgroundTemplate="{StaticResource blueTemplate}"
                PinkBackgroundTemplate="{StaticResource pinkTemplate}" />

 <card:SfCardLayout x:Name="cardLayout" BindableLayout.ItemsSource="{Binding Data}" BindableLayout.ItemTemplateSelector="{StaticResource dataTemplateSelector}" SwipeDirection="Left" VerticalOptions="Center"  HeightRequest="600" WidthRequest="300">
Also have attached the sample for your reference, please download it from the following location. 
Please let us know if you need further assistance on this. 


This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

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

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