Hiya,
As the title suggests I am trying to use a creation filter to add a button to the column header with an image. A button gets added fine however I do not get an image. I am trying to add the image to the button by using an 'Apperance' but it does not appear to do anything, though setting other properties such as the cursor does, I just don't get it, any help please?
An additional issue is I appear to have a floating button, that randomly docks to the right for no reason. No idea what this is, it occurs even if I change the if statement to affect only one column.
My code is below:
Public Sub AfterCreateChildElements(parent As Infragistics.Win.UIElement) Implements Infragistics.Win.IUIElementCreationFilter.AfterCreateChildElements
Dim aButtonUIElement As ButtonUIElement = CType(parent.GetDescendant(GetType(ButtonUIElement)), ButtonUIElement)
If IsNothing(aButtonUIElement) = True Then
aButtonUIElement = New ButtonUIElement(parent)
End If
If IsNothing(aButtonUIElement.GetAncestor(GetType(HeaderUIElement))) = True Then
Exit Sub
Dim aColumnHeader As ColumnHeader = CType(aButtonUIElement.GetAncestor(GetType(HeaderUIElement)), HeaderUIElement).GetContext(GetType(ColumnHeader))
If IsNothing(aColumnHeader) = False Then
If Strings.Left(aColumnHeader.Column.Key, 5) = "Staff" And aColumnHeader.Column.Key <> "Staffing" And aColumnHeader.Column.Key <> "StaffAllocatedHours" Then
If TypeOf parent Is HeaderUIElement = True Then
RemoveHandler aButtonUIElement.ElementClick, AddressOf _frmStaffing.OnButtonHeaderElementClick
AddHandler aButtonUIElement.ElementClick, New UIElementEventHandler(AddressOf _frmStaffing.OnButtonHeaderElementClick)
Dim anAppearance As New Infragistics.Win.Appearance
anAppearance.Cursor = Cursors.Cross 'Test to check anAppearance works, it does, the cursor changes to a cross.
anAppearance.Image = My.Resources.edit_formula_add 'However the image is not shown.
parent.ChildElements.Add(aButtonUIElement)
Dim rect As Rectangle = parent.RectInsideBorders
' adjust it slightly so that it fills the entire area of the row preview
rect.Offset((parent.Rect.Width / 5) * 4, -2)
' set the width of the button's rect
rect.Width = parent.Rect.Width / 5
' increase the height for aesthetic reasons
rect.Height = rect.Height + 2
' set the button element's rectangle
aButtonUIElement.Rect = rect
' set the appearance to draw the image.
aButtonUIElement.Appearance = anAppearance
End Sub
Note: I know its a bad idea to create the appearance object in this method so I have moved that and the event that sets its image into class's new() method now.
Its another one of my fixed it myself after lots of desk and head contact.
I found something in the documentation (I had searched before but must have missed it, which almost did exactly what I needed). I also found separately I needed to set ThemedElementAlpha to transparent which was causing my image not to display. Here is my final code, mainly shamelessly copied from the help documentation, which got my buttons to work the way I wanted:
Public Sub New(ByRef theStaffingForm As frmStaffingMatrix)
_StaffButtonAppearance.ThemedElementAlpha = Alpha.Transparent
_StaffButtonAppearance.ImageBackground = My.Resources.Calculator1
_frmStaffing = theStaffingForm
Dim column As UltraGridColumn
Dim child As UIElement
Dim rc As Rectangle
Dim childRect As Rectangle
Dim elementToAdd As ButtonUIElement
' If the parent element is not a cell element return
If (Not (TypeOf (parent) Is HeaderUIElement)) Then Return
' Get the associated column
column = parent.GetContext(GetType(UltraGridColumn))
' Return if the column isn’t the one we want to add the button to.
If column Is Nothing Then Exit Sub
If Strings.Left(column.Key, 5) <> "Staff" Or column.Key = "Staffing" Or column.Key = "StaffAllocatedHours" Then Exit Sub
' Create a new button element
elementToAdd = New ButtonUIElement(parent)
' hook into its click event
AddHandler elementToAdd.ElementClick, New UIElementEventHandler(AddressOf _frmStaffing.OnButtonHeaderElementClick)
' Get the rect of the parent element inside its borders
rc = parent.RectInsideBorders
rc.Offset(parent.Rect.Width - 24, 0)
' Set the width of the button's rect
rc.Width = 20
' Set the button element's rect
elementToAdd.Rect = rc
' Loop over the child elements and adjust their
' rects so they don't overlap the button
For Each child In parent.ChildElements
childRect = child.Rect
If (childRect.Right < rc.Left) Then
childRect.Width -= rc.Left - childRect.Right
childRect.X += rc.Left - childRect.Right
child.Rect = childRect
Next
elementToAdd.Appearance = _StaffButtonAppearance
' Append the button element to the
' child elements collection
parent.ChildElements.Add(elementToAdd)
Hello Alex,
Thank you for contacting us and congrats on figuring out a solution. Let us know if you have any additional questions.