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

定义

详细信息视图时,会发生ListView绘制并OwnerDraw属性设置为trueOccurs 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属性设置为trueView属性设置为View.DetailsIt 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.

适用于

另请参阅