vb.netdevexpress

DevExpress ItemsView.QueryItemTemplate endless loop


i'm trying to understand the ChatDemo from DevExpress. In my Code i have 2 Lists (Contacts,Messages), which i use as the DataSource.

Now to my Problem, my List contains 2 Objects of Messages. To show each individually i use the ItemsView.QueryItemTemplate Event. Like in the Demo Version.

But my Code jumps into a Endless Loop and always jumps between, QueryItemTemplate and the OnCustomizeItemEvent.

Code from Demo Version

Private Sub OnQueryItemTemplate(ByVal sender As Object, ByVal e As QueryItemTemplateEventArgs)
    Dim message = TryCast(e.Row, DevAV.Chat.Model.Message)
    If message Is Nothing Then Return
    If message.IsOwnMessage Then
        Styles.MyMessage.Apply(e.Template)
    Else
        Styles.Message.Apply(e.Template)
    End If

    Dim fluent = mvvmContext.OfType(Of MessagesViewModel)()
    fluent.ViewModel.OnMessageRead(message)
End Sub

Private Sub OnCustomizeItem(ByVal sender As Object, ByVal e As CustomizeItemArgs)
    Dim message = TryCast(e.Row, DevAV.Chat.Model.Message)
    If message Is Nothing Then Return
    If message.IsLiked Then
        Dim btnLike = e.Element.FindElementById("btnLike")
        Dim btnMore = e.Element.FindElementById("btnMore")
        If btnLike IsNot Nothing AndAlso btnMore IsNot Nothing Then
            btnLike.Hidden = False
            btnMore.Hidden = True
        End If
    End If

    If message.IsFirstMessageOfBlock Then Return
    If Not message.IsOwnMessage Then
        Dim avatar = e.Element.FindElementById("avatar")
        If avatar IsNot Nothing Then avatar.Style.SetVisibility(Utils.Html.Internal.CssVisibility.Hidden)
    End If

    Dim name = e.Element.FindElementById("name")
    If name IsNot Nothing Then name.Hidden = True
    If Not message.IsFirstMessageOfReply Then
        Dim sent = e.Element.FindElementById("sent")
        If sent IsNot Nothing Then sent.Hidden = True
    End If
End Sub

My Code:

    Private Sub IvMessages_QueryItemTemplate(sender As Object, e As DevExpress.XtraGrid.Views.Items.QueryItemTemplateEventArgs) Handles IvMessages.QueryItemTemplate
        Dim Message = TryCast(IvMessages.GetRow(e.RowHandle), MessageTest)

        If Message Is Nothing Then Return

        If Message.UserName = "Angelo" Then
            Dim Html = File.ReadAllText("C:\Users\Admin\Documents\Visual Studio 2022\Projects\SDSoftdent\SDControls\FrmChat\Resources\Html\OwnMessage.html")
            Dim CSS = File.ReadAllText("C:\Users\Admin\Documents\Visual Studio 2022\Projects\SDSoftdent\SDControls\FrmChat\Resources\Css\OwnMessage.css")
            IvMessages.HtmlTemplate.Template = Html
            IvMessages.HtmlTemplate.Styles = CSS
        Else
            Dim Html = File.ReadAllText("C:\Users\Admin\Documents\Visual Studio 2022\Projects\SDSoftdent\SDControls\FrmChat\Resources\Html\Message.html")
            Dim CSS = File.ReadAllText("C:\Users\Admin\Documents\Visual Studio 2022\Projects\SDSoftdent\SDControls\FrmChat\Resources\Css\Message.css")
            IvMessages.HtmlTemplate.Template = Html
            IvMessages.HtmlTemplate.Styles = CSS
        End If
    End Sub

I also tested to put the OnCustomizeEvent in a comment, but it does nothing, still loop.

Anyone a idea?

Docu from DevExpress: https://docs.devexpress.com/WindowsForms/DevExpress.XtraGrid.Views.Items.ItemsView.QueryItemTemplate


Solution

  • The root of the issue you described is incorrect usage of the event - you're modifying the entire View's template settings (the ItemsView.HtmlTemplate property) instead of customizing e.Template event's parameter:

    ' incorrect approach
    IvMessages.HtmlTemplate.Template = Html
    IvMessages.HtmlTemplate.Styles = CSS
    ' correct code
    e.Template.Template = Html
    e.Template.Styles = CSS.
    

    It forces the ItemsView to reload the entire layout and fire all customization events again for each item of View.