Back to Devexpress

BaseListBoxControl.CustomItemTemplate Event

windowsforms-devexpress-dot-xtraeditors-dot-baselistboxcontrol-075832a6.md

latest4.0 KB
Original Source

BaseListBoxControl.CustomItemTemplate Event

Allows you to assign custom templates to listbox items.

Namespace : DevExpress.XtraEditors

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Behavior")]
public event CustomItemTemplateEventHandler CustomItemTemplate
vb
<DXCategory("Behavior")>
Public Event CustomItemTemplate As CustomItemTemplateEventHandler

Event Data

The CustomItemTemplate event's data class is DevExpress.XtraEditors.CustomItemTemplateEventArgs.

Remarks

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.

Assign HTML-CSS Templates to Items

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.

Example

The following code assigns a custom HTML template (htmlTemplate2) to items that have no images.

csharp
private void listBoxControl1_CustomItemTemplate(object sender, DevExpress.XtraEditors.CustomItemTemplateEventArgs e) {
    var item = e.Item as TodoItem;
    if (!item.HasImage)
        e.HtmlTemplate = htmlTemplate2;
}
vb
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

Assign Regular Templates to Items

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

Templates

CustomizeItem

BaseListBoxControl Class

BaseListBoxControl Members

DevExpress.XtraEditors Namespace