Conditionally change backgroundcolor for captions in an ultragrid

415 Views Asked by At

I am using ultragrid 9.1. I am displaying the details as cardview. I can change the back color of the card caption by using the following property:

Ultragrid.DisplayLayout.Override.CardCaptionAppearance.BackColor = System.Drawing.Color.Red

However, I want to change the back color of the caption conditionally and not for all rows. I am unable to find the relevant property to set this.

1

There are 1 best solutions below

0
On

My problem has been solved. There is no in built property that could be used to set the background color of the caption. I had to use the DrawFilter interface.

You can find more information about this interface from this link:

You should create a class that implements IUIElementDrawFilter. In the GetPhasesToFilter method of the interface check if the element is CardCaptionUIElement and if it is return its BeforeDrawBackColor phase. Then in the DrawElement method you can draw the background using the DrawBackColor method of the drawParams argument.

Then, set the drawfilter for the ultragrid.

 UltraGrid1.DrawFilter = New CustomDrawFilter()

Class CustomDrawFilter : Implements IUIElementDrawFilter

Public Function DrawElement(drawPhase As DrawPhase, ByRef drawParams As UIElementDrawParams) As Boolean Implements IUIElementDrawFilter.DrawElement
    Dim row = DirectCast(drawParams.Element.GetContext(), UltraGridRow)

    If (row.VisibleIndex Mod 2 = 0 And row.Selected = False) Then
        drawParams.AppearanceData.BackColor = Color.Red
        drawParams.DrawBackColor(drawParams.Element.RectInsideBorders)
        Return True
    End If

    Return False
End Function

Public Function GetPhasesToFilter(ByRef drawParams As UIElementDrawParams) As DrawPhase Implements IUIElementDrawFilter.GetPhasesToFilter
    If (TypeOf (drawParams.Element) Is CardCaptionUIElement) Then
        Return DrawPhase.BeforeDrawBackColor
    Else
        Return DrawPhase.None
    End If

End Function
End Class