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

Customize the background color in SfComboBox

Hello!
I use Xamarin.Forms with the SfComboBox component.
I need to customize the background of each combobox item based on a value in my class.

The datasource of the combobox is prrenched with a List <Product>.

The Product class is composed by: int idproduct, string dsproduct, int amount.

I need to change the background color of each item based on the amount field, for example: if amount <= 0 the background should be red, if amount > 0 the background should be blue.

Does anyone have an example of how to do this?
Can you help?

5 Replies

MK Muneesh Kumar G Syncfusion Team July 5, 2019 07:19 AM UTC

Hi Julio, 
 
Greetings from Syncfusion.  
 
We have analyzed your requirement and you can achieve this by using DataTemplateSelector support, it has been implemented in our latest 17.2.0.28 Beta version.  
 
We have prepared a sample for the same. Please have the sample from the below link, 
 
 
Code Snippet: 
XAML: 

<ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="default">
.. 
            </DataTemplate>
            <DataTemplate x:Key="specific">
..
            </DataTemplate>
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
    <StackLayout>
        <Label Text="High performance Mobiles" FontSize="Medium" FontAttributes="Bold"/>
        <Label Text="Choose a Device to place order" FontAttributes="None" FontSize="Micro"/>
            <combobox:SfComboBox DataSource="{Binding Data}" x:Name="comboBox" DisplayMemberPath="dsproduct">
                <combobox:SfComboBox.ItemTemplate>
                    <local:MyDataTemplateSelector 
                        DefaultTemplate="{StaticResource default}" 
                        SpecificDataTemplate="{StaticResource specific}"/>
                </combobox:SfComboBox.ItemTemplate>
            </combobox:SfComboBox>
        </StackLayout>
    </ContentPage.Content>
</ContentPage> 
  
C#  
 
 protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
        {
            var message = item as Product;
           
            return message.amount <= 0 ? DefaultTemplate : SpecificDataTemplate;
        } 
 
 
Screenshot :  
 
Please check with the sample and let us know if you have any concern. 
 
Regards, 
Muneesh Kumar G.  
 



JC Julio Cézar July 8, 2019 12:32 PM UTC

Thank you


MK Muneesh Kumar G Syncfusion Team July 8, 2019 01:30 PM UTC

Hi Julio,  
 
Please let us know if you have any other queries.  
 
Regards, 
Muneesh Kumar G. 
 



JC Julio Cézar July 8, 2019 01:34 PM UTC

At the moment, none.
Everything working perfectly.
Thank you.


MK Muneesh Kumar G Syncfusion Team July 8, 2019 01:43 PM UTC

Hi Julio,   
  
Thank you.  
  
Regards,  
Muneesh Kumar G.  
 


Loader.
Live Chat Icon For mobile
Up arrow icon