New to Telerik UI for .NET MAUI? Start a free 30-day trial
Changing Background Color of RadComboBox Based on Selected Item
Updated over 6 months ago
Environment
| Version | Product | Author |
|---|---|---|
| 11.0.1 | Telerik UI for .NET MAUI ComboBox | Dobrinka Yordanova |
Description
I want to change the background color of the entire ComboBox control based on the selected item. The text always appears with a white background, and I need to apply a specific background color to the entire control.
This knowledge base article also answers the following questions:
- How to apply different background colors to ComboBox items in .NET MAUI?
- How to use
SelectionBoxTemplateto customizeRadComboBoxbackground? - How to make the
RadComboBoxreflect selected item background color?
Solution
To change the background color of the RadComboBox based on the selected item, use the SelectionBoxTemplate property, which is available when the control is non-editable (IsEditable set to False). The SelectionBoxTemplate allows you to customize the appearance of the selected item.
- Set the
IsEditableproperty toFalse. - Define a
SelectionBoxTemplatewith aDataTemplateto specify the background color of the selected item. - Use the
SelectedItemTemplateandItemTemplateto provide consistent styling for all items.
Here is an example:
xaml
<VerticalStackLayout>
<telerik:RadComboBox ItemsSource="{Binding Items}"
DisplayMemberPath="Name"
IsEditable="False"
Placeholder="Select City">
<telerik:RadComboBox.ItemTemplate>
<DataTemplate>
<telerik:RadBorder BackgroundColor="LightYellow"
MinimumWidthRequest="300">
<Label Text="{Binding Name}"
Padding="8, 7, 0, 7"
TextColor="Black"/>
</telerik:RadBorder>
</DataTemplate>
</telerik:RadComboBox.ItemTemplate>
<telerik:RadComboBox.SelectedItemTemplate>
<DataTemplate>
<telerik:RadBorder BackgroundColor="LightBlue"
MinimumWidthRequest="300">
<VerticalStackLayout>
<Label Text="{Binding Name}"
Padding="8, 7, 0, 7"
TextColor="Black"/>
<Label Text="{Binding Population}"
FontSize="12"
Padding="8, 7, 0, 7"/>
</VerticalStackLayout>
</telerik:RadBorder>
</DataTemplate>
</telerik:RadComboBox.SelectedItemTemplate>
<telerik:RadComboBox.SelectionBoxTemplate>
<DataTemplate>
<telerik:RadBorder BackgroundColor="LightBlue"
MinimumWidthRequest="300">
<VerticalStackLayout>
<Label Text="{Binding Name}"
Padding="8, 7, 0, 7"
TextColor="Black"/>
<Label Text="{Binding Population}"
FontSize="12"
Padding="8, 7, 0, 7"/>
</VerticalStackLayout>
</telerik:RadBorder>
</DataTemplate>
</telerik:RadComboBox.SelectionBoxTemplate>
</telerik:RadComboBox>
</VerticalStackLayout>
- The
ItemTemplatesets the background color for the items in the dropdown list. - The
SelectedItemTemplatespecifies the appearance of the selected item in the dropdown list. - The
SelectionBoxTemplateapplies the background color to the non-editable view of the selected item, ensuring consistency.