Version

Implementing Custom UIElement Objects

Users of Infragistics controls don’t need to know how UIElements are implemented in order to use the controls in their projects.

However, in order to create custom controls based on the PLF or to add/replace elements using a UIElementCreationFilter a more detailed understanding is necessary. If you don’t plan on creating your own controls and/or UIElements you can skip this section.

This section provides a brief look into how UIElements are implemented. It details the virtual properties and methods that can be overridden to control how an element behaves.

In order to create a custom element you must derive a class from the UIElement base class or a class that derives from it. Then override one or more of its virtual properties and/or methods to achieve the element’s desired look and/or behavior.

The following are a few element classes implemented in the PLF that might be useful to use as base classes:

  • AdjustableUIElement — this is useful as the base class for elements that can be moved or resized by the user (e.g. a column header). It exposes some additional virtual methods and properties to specify if the element supports left/right and/or up/down adjustments as well as adjustment range limits. It has logic for capturing the mouse and inverting and appropriate line/rectangle during the resize operation. In addition to exposing an 'ElementAdjusted' event it has a virtual 'ApplyAdjustment' method that is passed a delta value. This method is overridden to do the actual adjustment.

  • ButtonUIElement — this can be used as is or as a base class. It implements all of the functionality of a button and exposes a 'ButtonStyle' enumeration property so that it can support more than a dozen button styles.

  • CheckBoxUIElement - this can be used as is or as a base class.

  • ControlUIElementBase — this class must be used as the base class for the main UIElement of a control. The control must have a single instance of this element as its primary UIElement. In other words, elements derived from ControlUIElementBase must not have parent elements. It has a virtual 'OnPreMouseDown' method that is called when the user clicks on the mouse. This can be overridden to perform special logic (such as setting focus to the control) or to prevent the default mouse down logic from executing by returning True. Also, if the control makes use of KeyActionMappings (see section on 'Keyboard Handling), it needs to override the read-only 'CurrentState' property and the 'PerformKeyAction' method.

  • EmbeddableUIElementBase — this is the base class that embeddable editors derive their element classes from (see the section on 'Embeddable Editors').

  • ImageUIElement — this can be used as is or as a base class. It does not draw its background or borders. It simply displays a foreground image.

  • SplitterUIElement — this class derives from AdjustableUIElement and implements all of the functionality of a splitter bar. Usually a derived class just needs to override the 'ApplyAdjustment' method.

  • SplitterIntersectionUIElement — this element represents the intersection of a vertical and horizontal SplitterUIElement. It has logic for applying the appropriate adjustment to both splitters when the user drags the intersection element diagonally. It also listens to the ElementAdjusted event of both splitter elements to reposition itself automatically.

  • TextUIElement — this can be used as is or as a base class. It does not draw its background or borders. It simply displays text.

The following two tables list the virtual properties and methods that are exposed by the UIElement base class.

Virtual UIElement Properties

Property Name Type Description Value returned by the base UIElement class

Boolean

Returns True if this element can be moved or resized by the user. This property is read-only.

False

Border3DSide

Bit flags indicating which sides the borders should be drawn on (e.g. left, right, top and/or bottom

All

UIElementBorderStyle

Specifies the style of border that needs to be drawn (e.g. solid, dotted, inset, raised, none etc.). This property is read-only.

None

Boolean

Indicates if capture for this element should be terminated if the escape key is pressed. This property is read-only.

True

Integer

Returns the expected number of child elements for this element. This should be overridden for elements that will have a lot of children to optimize the child collection allocations This property is read-only.

3

Boolean

Determines if the drawing for this element’s child elements will be clipped inside the borders of this element. This should only return true for those elements that need it since there is some execution overhead to intersecting the clip region. This property is read-only.

False

Rectangle

Returns the rectangle that will be used to clip child elements if ClipChildren returns True. This property is read-only.

RectInsideBorders

Boolean

Determines if the drawing for this element will be clipped so that it can’t draw outside its area. This should only return True for those elements that need it since there is some execution overhead to intersecting the clip region. This property is read-only.

False

Windows.Forms.Control

Returns the control associated with this element. This property is read-only.

Walks up the parent chain until it reaches the main control element and returns the associated control.

Windows.Forms.Cursor

Returns the cursor that should be used when the mouse is over the element.

Calls InitAppearance to get any assigned cursor. If none is set, it returns its parent element’s cursor.

Boolean

If, during a drawing operation, this property returns true and the control has input focus (or the forceDrawAsFocused parameter was true on the call to the Draw method) then the DrawFocus method will be called on this element. This property is read-only.

False

Boolean

Returns true if the element is enabled. This property is read-write.

Returns false if Enabled is false. Otherwise it returns its parent element’s Enabled property. This effectively walks up the parent chain until it reaches the main control element which overrides this property to return the Enabled property of the associated control.

Boolean

Returns true if this element should be drawn. This property is read-only. Note: returning false for this property will prevent this element and all of its child elements from being drawn.

True

Rectangle

The elements bounding rectangle in client coordinates of the control. This property is read-write.

The element’s Rect. Setting this property will mark the child elements collection dirty which will trigger a call to PositionChildElements on the next paint.

Rectangle

The elements rectangle within its borders in client coordinates of the control. This property is read-only.

The element’s rect adjusted for its borders based on the BorderStyle and BorderSides properties.

System.Drawing.Region

The region of the element. It is called during a drawing operation if the ClipSelf property returns true. Override this property to return an irregular shaped region.

A rectangular region that must be disposed by the caller.

ISelectableItem

Returns an object that implements the ISelectableItem interface or Null. The SelectableItem is used by the SelectionStrategy to select individual items and/or ranges of items. This property is read-only.

The PrimaryContext if it implements the ISelectableItem interface. Otherwise it returns the SelectableItem property of its parent element.

Boolean

Return True for those elements that want to support horizontal mouse panning. If an element overrides this property it should also override the OnMousePanHorizontal method to perform the pan operation. This property is read-only.

False

Boolean

Return True for those elements that want to support vertical mouse panning. If an element overrides this property it should also override the OnMousePanVertical method to perform the pan operation. This property is read-only.

False

Boolean

Return True if the element wants its OnMouseHover method to be called when the mouse is paused over the element or one of its child elements. This property is read-only.

Note
Note
When the mouse is paused over an element the PLF logic first checks this property on the element. If the property returns false it then walks up the element’s parent chain and calls OnMouseHover on the first element that returns true for this property.

False

Virtual UIElement Methods

Method Name Description Default implementation in the base UIElement class

Contains

Returns:

  • Boolean

Parameters:

  • Point (in client coordinates)

Override this method to support irregular regions.

Returns True if it is in the element’s rectangle.

DrawBackColor

Returns:

  • Void

Parameters:

  • UIElementDrawParams

This method is called after DrawTheme and during a drawing operation to draw an element’s background color. It is usually overridden to provide a Null implementation that prevents the element’s background from being drawn (e.g. the TextUIElement does this). It can also be used to perform custom drawing.

Calls the DrawBackColor method with the passed-in UIElementDrawParams structure to fill the invalid area.

DrawBorders

Returns:

  • Void

Parameters:

  • UIElementDrawParams

This method is called after DrawImageBackground during a drawing operation to draw an element’s borders. It is actually very rare that this method would need to be overridden since the BorderStyle and BorderSides properties allow very flexible control of the borders. However, it can be overridden to perform custom border drawing.

Calls the DrawBorders method with the passed-in UIElementDrawParams structure, which draws the borders based on the BorderStyle and BorderSides properties of the element.

DrawChildElements

Returns:

  • Void

Parameters:

  • UIElementDrawParams

This method is called after DrawForeground during a drawing operation to draw an element’s child elements. It is usually overridden to provide a Null implementation to prevent the element’s child elements from drawing.

Draws the child elements.

DrawFocus

Returns:

  • Void

Parameters:

  • UIElementDrawParams

This method is called during a drawing operation to draw a focus indicator for the element. It is called after the element and all of its child elements have been drawn. It is called only if the control has focus (or the forceDrawAsFocused parameter was set to True on the call to the Draw method) and the element returns True from the DrawsFocusRect property.

Draws a focus rectangle inside the borders of the element using the RectInsideBorders property.

DrawForeground

Returns:

  • Void

Parameters:

  • UIElementDrawParams

This method is called after DrawImage during a drawing operation to draw an element’s foreground (e.g. the TextUIElement draws the text in this method).

Does nothing.

DrawImage

Returns:

  • Void

Parameters:

  • UIElementDrawParams

This method is called after DrawBorders during a drawing operation to draw an element’s foreground image (e.g. the ImageUIElement draws the image in this method).

Does nothing.

DrawImageBackground

Returns:

  • Void

Parameters:

  • UIElementDrawParams

This method is called after DrawBackColor during a drawing operation to draw an element’s background image. It is usually overridden to provide a Null implementation to prevent the element’s background from being drawn (e.g. the TextUIElement does this). It can also be used to perform custom drawing.

Draws the ImageBackground of the resolved appearance which is exposed by the passed-in UIElementDrawParams structure.

DrawTheme

Returns:

  • Boolean - True will cause the other 'Draw…​' methods to be bypassed (except DrawChildElements)

Parameters:

  • UIElementDrawParams

This is the first 'Draw…' method called during a drawing operation to draw the element as an XP themed element. Themed elements like drop-down buttons and scrollbar elements override this method and use the XPThemes class to render the element appropriately and return true so that the other draw methods are bypassed.

Does nothing and returns False so the other 'Draw…' methods will be called.

GetAdjustableCursor

Returns:

  • Cursor

Parameters:

  • Point (in client coordinates)

This method is overridden by elements that return True from the Adjustable property to supply the appropriate cursor when the mouse is over an adjustable area of the element .

Returns Null.

GetAdjustmentRange

Returns:

  • Void

Parameters:

  • Point (in client coordinates) out

  • UIElementAdjustmentRangeParams range

The range parameter is set to a structure that contains the range limits for adjusting the element in either or both dimensions. It also returns the initial rectangles for the vertical and horizontal bars that need to be inverted during a mouse drag operation.

Sets the range parameter to a structure filled with zeroes.

GetContext

Returns:

  • An object of the requested type or null.

Parameters:

  • Type

This method returns the context for the element. Overriding this method is not very common at all. It is normally overridden only by elements that have more than one context object (which means the base class’s PrimaryContext property is inadequate). For example, in UltraGrid, the only element to override this method is the RowColRegionIntersectionUIElement which must maintain a reference on both a RowScrollRegion object and a ColScrollRegion object.

Checks if the context object has been set and if its type matches the requested type. If so, it returns the context object, if not, it calls this method on its parent (which effectively walks up the parent chain until it finds a match or reaches the control’s main element). This is important because if, for example, you ask for the Row context of a CellUIElement’s child TextUIElement, it will walk up the parent element chain until it reaches the RowUIElement and return the appropriate Row context.

InitAppearance

Returns:

  • Void

Parameters:

  • AppearanceData

  • AppearancePropFlags

Called before an element is drawn so that the element can specify its fore color, back color, border color etc. Note: any appearance settings left to their default values will pick up the parent element’s settings (except for the ImageBackground, BackGradientStyle and BackHatchStyle properties).

Does nothing.

Offset

Returns:

  • Void

Parameters:

  • integer deltaX

  • integer deltaY

  • Boolean recursive

Normally there is no reason to override this method.

Offsets this element’s rectangle and if the recursive flag is true calls this method on each of its child elements.

OnAfterDraw

Returns:

  • Void

Parameters:

  • UIElementDrawParams

Called after an element has been drawn.

Does nothing

OnBeforeDraw

Returns:

  • Void

Parameters:

  • None

Called before an element is about to be drawn.

Does nothing

OnCaptureAborted

Returns:

  • Void

Parameters:

  • None

Called when mouse capture is cancelled on the element that captured the mouse (e.g. in the OnMouseDown).

Stops any panning timers and resets the cursor and then calls this method on its parent element which effectively just walks up the parent chain.

OnClick

Returns:

  • Void

Parameters:

  • None

Called when mouse is clicked on an element.

Calls this method on its parent element which effectively just walks up the parent chain.

OnDoubleClick

Returns:

  • Void

Parameters:

  • None

Called when mouse is double-clicked on an element.

Calls this method on its parent element, which effectively just walks up the parent chain.

OnMouseDown

Returns:

  • Boolean - returning true bypasses default logic.

Parameters:

  • MouseEventArgs e

  • Boolean adjustableAreaRef

  • UIElement captrueMouseForElement

This method is called when a mouse button is pressed while the mouse is over the element. It is normally overridden to perform some action and/or capture the mouse. Setting the captureMouseForElement parameter to an element (usually this element) before returning from this method will capture the mouse for the element and cause subsequent OnMouseMove calls to be made on the element.

If the middle mouse button has been pressed and either the SupportsHorizontalMousePanning or the SupportsVerticalMousePanning properties return True, then the appropriate mouse panning cursor is displayed and a timer is started to generate OnMousePanHorizontal and/or OnMousePanVertical method calls and the method returns True. Otherwise it calls this method on its parent element (effectively walking up the parent chain) until an element overrides the method or the main element is reached and False is returned.

OnMouseEnter

Returns:

  • Void

Parameters:

  • None

This method is called when the mouse enters the element’s rectangle.

Does nothing.

OnMouseHover

Returns:

  • Void

Parameters:

  • None

This method is normally overridden to show tool tips. It is called when the mouse is paused over the element. If an element overrides this method it should also override the WantsMouseHoverNotification property and return true.

Calls this method on its parent element which effectively just walks up the parent chain.

OnMouseLeave

Returns:

  • Void

Parameters:

  • None

This method is called when the mouse leaves the element’s rectangle.

Does nothing.

OnMouseMove

Returns:

  • Void

Parameters:

  • MouseEventArgs e

Called when the mouse is moved over an element or the mouse is moved and the element captured the mouse in OnMouseDown.

Calls this method on its parent element which effectively just walks up the parent chain.

OnMousePanHorizontal

Returns:

  • Void

Parameters:

  • Integer

This method is called during a horizontal panning operation by elements that return true for their SupportsHorizontalMousePanning property. The number of pixels between the cursor position and the origin mark is passed into this method. This value is positive when the cursor is to the right of the origin mark, negative when it is left of the origin mark and zero if it is within the origin mark threshold.

Does nothing. Should be overridden by elements that return true for their SupportsHorizontalMousePanning property.

OnMousePanVertical

Returns:

  • Void

Parameters:

  • Integer

This method is called during a vertical panning operation by elements that return true for their SupportsVerticalMousePanning property. The number of pixels between the cursor position and the origin mark is passed into this method. This value is positive when the cursor is below the origin mark, negative when it is above the origin mark and zero if it is within the origin mark threshold.

Does nothing. Should be overridden by elements that return true for their SupportsVerticalMousePanning property.

OnMouseUp

Returns:

  • Boolean

Parameters:

  • MouseEventArgs e

Called when the mouse is released over the element or the mouse is released and the element captured the mouse in OnMouseDown. Note: returning true from this method will ignore the next click event.

Stops any panning timers and resets the cursor and then calls this method on its parent element which effectively just walks up the parent chain.

PointInAdjustableArea

Returns:

  • Boolean

Parameters:

  • Point (in client coordinates)

This method is overridden by elements that return True from the Adjustable property to determine if a point is over an adjustable area of the element.For example, a column header element may want to allow resizing of its width if the user clicking within 3 pixels of its right border. In this case if the point was within 3 pixels of the right border this method would return true.

Returns False.

PositionChildElements

Returns:

  • Void

Parameters:

  • None

This is called when an element needs to create/position its child elements. This method must be overridden by any element that has child elements.

Does nothing (no child elements are created).

VerifyChildElements

Returns:

  • Void

Parameters:

  • ControlUIElementBase control Element

  • Boolean recursive

Called during a drawing operation to ensure that all child elements are created and positioned properly. Normally this method does not need to be overridden.

If the ChildElementsDirty flag is True then the default implementation will call PositionChildElements and reset the flag.