Skip to content

Commit ddd2e79

Browse files
dimodiDimo Dimov
andauthored
docs: Add KB for custom dropdown item background (#543)
Co-authored-by: Dimo Dimov <dimo@Dimos-MacBook-Pro.local>
1 parent fe386a7 commit ddd2e79

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
title: Change the Item Background Color of the ComboBox and DropDownList
3+
description: How to change the background color of the items in the ComboBox dropdown?
4+
type: how-to
5+
page_title: Use Custom Background Color for the ComboBox and DropDownList Items
6+
slug: dropdowns-custom-item-background
7+
position:
8+
tags: item, custom, background, color, combobox, dropdown, dropdownlist
9+
ticketid: 1540151
10+
res_type: kb
11+
---
12+
13+
## Environment
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>AutoComplete, ComboBox, DropDownList, MultiSelect for Blazor</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
24+
## Description
25+
26+
How to change the background color of individual items in the ComboBox or DropDownList dropdown? I would also like to display the item's color in the component itself when that item is selected.
27+
28+
## Solution
29+
30+
It is possible to apply a custom background color to each item inside the ComboBox/DropDownList dropdown. To achieve that, use an [**ItemTemplate**]({%slug components/dropdownlist/templates%}). The same customization is possible for the AutoComplete and MultiSelect.
31+
32+
On the other hand, to apply a custom background color to the value inside the component, use a [**ValueTemplate**]({%slug components/dropdownlist/templates%}). This customization is supported for the DropDownList only.
33+
34+
Here are some additional notes to keep in mind, and an example.
35+
36+
* Use a wrapper element inside the `ItemTemplate` that will apply the background. Use custom CSS classes or inline styles for the custom backgrounds.
37+
* The dropdown items use flexbox. To make the wrapper element expand, set an explicit 100% width style for it.
38+
* The dropdown items have paddings that will create visible gaps between the items with custom backgrounds. Similar gaps will appear between the dropdown borders and the item template content. To remove these gaps, remove the built-in paddings and add them inside the `ItemTemplate`.
39+
* The custom background styles will override the default hover and selected styles of the items. Use additional custom CSS rules to re-apply those styles. If you will use inline styles for the custom background, you will need `!important` in the external CSS rules for the hover/selected styles.
40+
41+
>caption Apply custom background colors to dropdown items and the DropDownList component.
42+
43+
````CSHTML
44+
@* The same approach with PopupClass is possible with AutoComplete and MultiSelect *@
45+
46+
<TelerikComboBox Data="@ItemsToSelect"
47+
@bind-Value="@SelectedValue"
48+
TextField="@nameof(ModelWithColor.Text)"
49+
ValueField="@nameof(ModelWithColor.ID)"
50+
PopupClass="colored-dropdown">
51+
<ItemTemplate>
52+
<div class="item-wrapper" style="background: @context.Color;">
53+
@context.Text
54+
</div>
55+
</ItemTemplate>
56+
</TelerikComboBox>
57+
58+
<TelerikDropDownList Data="@ItemsToSelect"
59+
@bind-Value="@SelectedValue"
60+
Filterable="false"
61+
FilterOperator="StringFilterOperator.Contains"
62+
TextField="@nameof(ModelWithColor.Text)"
63+
ValueField="@nameof(ModelWithColor.ID)"
64+
PopupClass="colored-dropdown"
65+
Class="colored-input">
66+
<ItemTemplate>
67+
<div class="item-wrapper" style="background: @context.Color;">
68+
@context.Text
69+
</div>
70+
</ItemTemplate>
71+
<ValueTemplate>
72+
<span class="value-wrapper" style="background: @context.Color;">
73+
@context.Text
74+
</span>
75+
</ValueTemplate>
76+
</TelerikDropDownList>
77+
78+
<style>
79+
80+
/* START: Dropdowns for all components */
81+
82+
/* remove default padding to avoid gaps between the backgrounds */
83+
.colored-dropdown .k-list .k-item {
84+
padding: 0;
85+
}
86+
87+
/* expand custom background area and apply paddings inside */
88+
.colored-dropdown .item-wrapper {
89+
width: 100%;
90+
padding: .3em .6em;
91+
}
92+
93+
/* (re)apply hover state background */
94+
.colored-dropdown .k-item:hover .item-wrapper {
95+
background: #ccc !important;
96+
}
97+
98+
/* (re)apply selected state background */
99+
.colored-dropdown .k-item.k-state-selected .item-wrapper {
100+
background: #666 !important;
101+
}
102+
103+
/* END: Dropdowns for all components */
104+
105+
/* START: DropDownList */
106+
107+
.colored-input .k-input {
108+
padding: 0;
109+
}
110+
111+
.colored-input .value-wrapper {
112+
display: block;
113+
width: 100%;
114+
padding: 4px 8px;
115+
}
116+
117+
/* END: DropDownList */
118+
</style>
119+
120+
@code {
121+
int SelectedValue { get; set; } = 2;
122+
string SelectedString { get; set; } = "Text 2";
123+
124+
List<ModelWithColor> ItemsToSelect { get; set; } = new()
125+
{
126+
new ModelWithColor() { ID = 1, Text = "Text 1", Color = "#ffc" },
127+
new ModelWithColor() { ID = 2, Text = "Text 2", Color = "#cfc" },
128+
new ModelWithColor() { ID = 3, Text = "Text 3", Color = "#ccf" },
129+
};
130+
131+
public class ModelWithColor
132+
{
133+
public int ID { get; set; }
134+
public string Text { get; set; }
135+
public string Color { get; set; }
136+
}
137+
}
138+
````

0 commit comments

Comments
 (0)