windowsforms-devexpress-dot-xtraeditors-dot-baselistboxcontrol-075832a6.md
Allows you to assign custom templates to listbox items.
Namespace : DevExpress.XtraEditors
Assembly : DevExpress.XtraEditors.v25.2.dll
NuGet Package : DevExpress.Win.Navigation
[DXCategory("Behavior")]
public event CustomItemTemplateEventHandler CustomItemTemplate
<DXCategory("Behavior")>
Public Event CustomItemTemplate As CustomItemTemplateEventHandler
The CustomItemTemplate event's data class is DevExpress.XtraEditors.CustomItemTemplateEventArgs.
When item templates are enabled, the control’s default behavior is to generate list box items from the default template. The default template is the first element in the BaseListBoxControl.HtmlTemplates collection if this collection is not empty. Otherwise, the default template is the first element in the BaseListBoxControl.Templates collection.
You can handle the CustomItemTemplate event to assign templates to items dynamically, and thus override an item’s default template. You can use the BaseListBoxControl.HtmlTemplates and BaseListBoxControl.Templates collections to create item templates beforehand.
The currently processed item can be identified with the following properties:
e.Index — Specifies the item’s zero-based index in the item collection.e.Item — Specifies the item’s underlying data object.To assign an HTML-CSS template to the currently processed item, access the required template (for example, from the BaseListBoxControl.HtmlTemplates collection), and assign it to the e.HtmlTemplate event parameter.
The following code assigns a custom HTML template (htmlTemplate2) to items that have no images.
private void listBoxControl1_CustomItemTemplate(object sender, DevExpress.XtraEditors.CustomItemTemplateEventArgs e) {
var item = e.Item as TodoItem;
if (!item.HasImage)
e.HtmlTemplate = htmlTemplate2;
}
Private Sub ListBoxControl1_CustomItemTemplate(sender As Object, e As DevExpress.XtraEditors.CustomItemTemplateEventArgs) _
Handles ListBoxControl1.CustomItemTemplate
Dim item = TryCast(e.Item, TodoItem)
If Not item.HasImage Then
e.HtmlTemplate = htmlTemplate2
End If
End Sub
To assign a regular template to the item, access this template in the e.Templates or BaseListBoxControl.Templates collection (these collections are equivalent), and assign it to the e.Template event parameter.
HTML-CSS templates have priority over regular templates.
See Also