|
| 1 | +--- |
| 2 | +title: Changing Background Color of RadComboBox Based on Selected Item |
| 3 | +description: Learn how to change the background color of the RadComboBox for .NET MAUI depending on the selected item. |
| 4 | +type: how-to |
| 5 | +page_title: Change Background Color of RadComboBox Items in .NET MAUI |
| 6 | +meta_title: Change Background Color of RadComboBox Items Based on Selection in .NET MAUI |
| 7 | +slug: change-background-color-radcombobox-dotnet-maui |
| 8 | +tags: combobox,.net maui,background color,selectionboxtemplate |
| 9 | +res_type: kb |
| 10 | +ticketid: 1692680 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +| Version | Product | Author | |
| 16 | +| --- | --- | ---- | |
| 17 | +| 11.0.1 | Telerik UI for .NET MAUI ComboBox | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) | |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +I want to change the background color of the entire [ComboBox](https://docs.telerik.com/devtools/maui/controls/combobox/overview) 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. |
| 22 | + |
| 23 | +This knowledge base article also answers the following questions: |
| 24 | +- How to apply different background colors to ComboBox items in .NET MAUI? |
| 25 | +- How to use `SelectionBoxTemplate` to customize `RadComboBox` background? |
| 26 | +- How to make the `RadComboBox` reflect selected item background color? |
| 27 | + |
| 28 | +## Solution |
| 29 | + |
| 30 | +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. |
| 31 | + |
| 32 | +1. Set the `IsEditable` property to `False`. |
| 33 | +2. Define a `SelectionBoxTemplate` with a `DataTemplate` to specify the background color of the selected item. |
| 34 | +3. Use the `SelectedItemTemplate` and `ItemTemplate` to provide consistent styling for all items. |
| 35 | + |
| 36 | +Here is an example: |
| 37 | + |
| 38 | +```xaml |
| 39 | +<VerticalStackLayout> |
| 40 | + <telerik:RadComboBox ItemsSource="{Binding Items}" |
| 41 | + DisplayMemberPath="Name" |
| 42 | + IsEditable="False" |
| 43 | + Placeholder="Select City"> |
| 44 | + <telerik:RadComboBox.ItemTemplate> |
| 45 | + <DataTemplate> |
| 46 | + <telerik:RadBorder BackgroundColor="LightYellow" |
| 47 | + MinimumWidthRequest="300"> |
| 48 | + <Label Text="{Binding Name}" |
| 49 | + Padding="8, 7, 0, 7" |
| 50 | + TextColor="Black"/> |
| 51 | + </telerik:RadBorder> |
| 52 | + </DataTemplate> |
| 53 | + </telerik:RadComboBox.ItemTemplate> |
| 54 | + <telerik:RadComboBox.SelectedItemTemplate> |
| 55 | + <DataTemplate> |
| 56 | + <telerik:RadBorder BackgroundColor="LightBlue" |
| 57 | + MinimumWidthRequest="300"> |
| 58 | + <VerticalStackLayout> |
| 59 | + <Label Text="{Binding Name}" |
| 60 | + Padding="8, 7, 0, 7" |
| 61 | + TextColor="Black"/> |
| 62 | + <Label Text="{Binding Population}" |
| 63 | + FontSize="12" |
| 64 | + Padding="8, 7, 0, 7"/> |
| 65 | + </VerticalStackLayout> |
| 66 | + </telerik:RadBorder> |
| 67 | + </DataTemplate> |
| 68 | + </telerik:RadComboBox.SelectedItemTemplate> |
| 69 | + <telerik:RadComboBox.SelectionBoxTemplate> |
| 70 | + <DataTemplate> |
| 71 | + <telerik:RadBorder BackgroundColor="LightBlue" |
| 72 | + MinimumWidthRequest="300"> |
| 73 | + <VerticalStackLayout> |
| 74 | + <Label Text="{Binding Name}" |
| 75 | + Padding="8, 7, 0, 7" |
| 76 | + TextColor="Black"/> |
| 77 | + <Label Text="{Binding Population}" |
| 78 | + FontSize="12" |
| 79 | + Padding="8, 7, 0, 7"/> |
| 80 | + </VerticalStackLayout> |
| 81 | + </telerik:RadBorder> |
| 82 | + </DataTemplate> |
| 83 | + </telerik:RadComboBox.SelectionBoxTemplate> |
| 84 | + </telerik:RadComboBox> |
| 85 | +</VerticalStackLayout> |
| 86 | +``` |
| 87 | + |
| 88 | +- The `ItemTemplate` sets the background color for the items in the dropdown list. |
| 89 | +- The `SelectedItemTemplate` specifies the appearance of the selected item in the dropdown list. |
| 90 | +- The `SelectionBoxTemplate` applies the background color to the non-editable view of the selected item, ensuring consistency. |
| 91 | + |
| 92 | +## See Also |
| 93 | + |
| 94 | +- [ComboBox Overview](https://docs.telerik.com/devtools/maui/controls/combobox/overview) |
| 95 | +- [ComboBox Templates](https://docs.telerik.com/devtools/maui/controls/combobox/templates) |
0 commit comments