ListView.DrawSubItem ListView.DrawSubItem ListView.DrawSubItem ListView.DrawSubItem Event

定义

当绘制 ListView 的详细信息视图并且 OwnerDraw 属性设置为 true 时发生。Occurs when the details view of a ListView is drawn and the OwnerDraw property is set to true.

public:
 event System::Windows::Forms::DrawListViewSubItemEventHandler ^ DrawSubItem;
public event System.Windows.Forms.DrawListViewSubItemEventHandler DrawSubItem;
member this.DrawSubItem : System.Windows.Forms.DrawListViewSubItemEventHandler 
Public Custom Event DrawSubItem As DrawListViewSubItemEventHandler 

示例

下面的代码示例提供了DrawSubItem事件处理程序的实现。The following code example provides an implementation of a DrawSubItem event handler. 有关完整的示例,请参阅OwnerDraw参考主题。For the complete example, see the OwnerDraw reference topic.

// Draws subitem text and applies content-based formatting.
private void listView1_DrawSubItem(object sender,
    DrawListViewSubItemEventArgs e)
{
    TextFormatFlags flags = TextFormatFlags.Left;

    using (StringFormat sf = new StringFormat())
    {
        // Store the column text alignment, letting it default
        // to Left if it has not been set to Center or Right.
        switch (e.Header.TextAlign)
        {
            case HorizontalAlignment.Center:
                sf.Alignment = StringAlignment.Center;
                flags = TextFormatFlags.HorizontalCenter;
                break;
            case HorizontalAlignment.Right:
                sf.Alignment = StringAlignment.Far;
                flags = TextFormatFlags.Right;
                break;
        }

        // Draw the text and background for a subitem with a 
        // negative value. 
        double subItemValue;
        if (e.ColumnIndex > 0 && Double.TryParse(
            e.SubItem.Text, NumberStyles.Currency,
            NumberFormatInfo.CurrentInfo, out subItemValue) &&
            subItemValue < 0)
        {
            // Unless the item is selected, draw the standard 
            // background to make it stand out from the gradient.
            if ((e.ItemState & ListViewItemStates.Selected) == 0)
            {
                e.DrawBackground();
            }

            // Draw the subitem text in red to highlight it. 
            e.Graphics.DrawString(e.SubItem.Text,
                listView1.Font, Brushes.Red, e.Bounds, sf);

            return;
        }

        // Draw normal text for a subitem with a nonnegative 
        // or nonnumerical value.
        e.DrawText(flags);
    }
}
' Draws subitem text and applies content-based formatting.
Private Sub listView1_DrawSubItem(ByVal sender As Object, _
    ByVal e As DrawListViewSubItemEventArgs) _
    Handles listView1.DrawSubItem

    Dim flags As TextFormatFlags = TextFormatFlags.Left

    Dim sf As New StringFormat()
    Try

        ' Store the column text alignment, letting it default
        ' to Left if it has not been set to Center or Right.
        Select Case e.Header.TextAlign
            Case HorizontalAlignment.Center
                sf.Alignment = StringAlignment.Center
                flags = TextFormatFlags.HorizontalCenter
            Case HorizontalAlignment.Right
                sf.Alignment = StringAlignment.Far
                flags = TextFormatFlags.Right
        End Select

        ' Draw the text and background for a subitem with a 
        ' negative value. 
        Dim subItemValue As Double
        If e.ColumnIndex > 0 AndAlso _
            Double.TryParse(e.SubItem.Text, NumberStyles.Currency, _
            NumberFormatInfo.CurrentInfo, subItemValue) AndAlso _
            subItemValue < 0 Then

            ' Unless the item is selected, draw the standard 
            ' background to make it stand out from the gradient.
            If (e.ItemState And ListViewItemStates.Selected) = 0 Then
                e.DrawBackground()
            End If

            ' Draw the subitem text in red to highlight it. 
            e.Graphics.DrawString(e.SubItem.Text, _
                Me.listView1.Font, Brushes.Red, e.Bounds, sf)

            Return

        End If

        ' Draw normal text for a subitem with a nonnegative 
        ' or nonnumerical value.
        e.DrawText(flags)

    Finally
        sf.Dispose()
    End Try

End Sub

注解

此事件使你可以使用所有者描述自ListView定义控件的外观。This event lets you customize the appearance of a ListView control using owner drawing. 仅当OwnerDraw属性设置为true ,并且View属性设置为View.Details时,才会引发此方法。It is raised only when the OwnerDraw property is set to true and the View property is set to View.Details. 有关所有者描述的详细信息,请参阅OwnerDraw属性参考主题。For more information on owner drawing, see the OwnerDraw property reference topic.

备注

子项信息通常显示在图块视图和详细信息视图中,但在磁贴视图中,必须在DrawItem事件的处理程序中进行绘制。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.

DrawSubItem个子项ListView可能发生该事件。The DrawSubItem event can occur for each ListView subitem. 您可以处理DrawItem事件以绘制所有子项(如背景)公用的元素,并DrawSubItem处理事件以绘制单个子项(如文本值)的元素。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. 你还可以仅使用两个事件ListView中的一个来绘制控件中的所有项,但这可能不太方便。You can also draw all items in the ListView control using only one of the two events, although this may be less convenient. 若要在详细信息视图中绘制列标题,必须处理DrawColumnHeader事件。To draw column headers in the details view, you must handle the DrawColumnHeader event.

备注

对于DrawSubItem没有ColumnHeader对象已添加到集合中的任何子项,不会Columns发生该事件。The DrawSubItem event does not occur for any subitems for which no ColumnHeader object has been added to the Columns collection. 另请注意,每个ListViewItem对象的第一个子项表示父项本身,并显示在第一列中。Note also that the first subitem of each ListViewItem object represents the parent item itself, and is displayed in the first column.

有关处理事件的详细信息, 请参阅处理和引发事件For more information about handling events, see Handling and Raising Events.

适用于

另请参阅