ListView::OwnerDraw Property
Gets or sets a value indicating whether the ListView control is drawn by the operating system or by code that you provide.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
A ListView control is normally drawn by the operating system. In order to customize the appearance of ListView items, subitems, and column headers, set the OwnerDraw property to true and provide a handler for one or more of the following events: DrawItem, DrawSubItem, DrawColumnHeader. This is called owner drawing. When the View property is set to View::Details, all three events occur; otherwise, only the DrawItem event occurs.
Note |
|---|
Subitem information typically appears in the tile view as well as the details view, but in the tile view, it must be drawn in a handler for the DrawItem event. The DrawSubItem event does not occur for any subitems for which no ColumnHeader object has been added to the Columns collection. Note also that the first subitem of each ListViewItem object represents the parent item itself, and is displayed in the first column. |
In the details view, you can handle the DrawItem event to draw elements common to all subitems, such as the background, and handle the DrawSubItem event to draw elements for individual subitems, such as text values. You can also draw all items in the ListView control using only one of the two events, although this may be less convenient. To draw column headers in the details view, you must handle the DrawColumnHeader event.
Note |
|---|
To avoid issues with graphics flickering when owner drawing, override the ListView control and set the DoubleBuffered property to true. |
For more information about handling events, see Handling and Raising Events.
The following code example demonstrates how to provide custom drawing for a ListView control. The ListView control in the example has a gradient background. Subitems with negative values have a red foreground and a black background.
A handler for the DrawItem event draws the background for entire items. A handler for the DrawSubItem event draws the text values and both the text and background for subitems that have negative values. A handler for the DrawColumnHeader event draws each column header.
A ContextMenu component provides a way to switch between the details view and the list view. In the list view, only the DrawItem event is raised. In this case, the text and background are both drawn in the DrawItem event handler.
Available since 2.0
