通过


DrawListViewItemEventArgs.Item 属性

定义

获取要绘制的 ListViewItem

public:
 property System::Windows::Forms::ListViewItem ^ Item { System::Windows::Forms::ListViewItem ^ get(); };
public System.Windows.Forms.ListViewItem Item { get; }
member this.Item : System.Windows.Forms.ListViewItem
Public ReadOnly Property Item As ListViewItem

属性值

ListViewItem 绘制的。

示例

下面的代码示例演示如何在应用程序中使用 Item 为控件提供自定义绘图 ListView 的属性。 在此示例中,事件的处理程序 ListView.DrawItem 绘制整个项的背景。 除详细信息视图之外的所有视图中,此处理程序还会绘制前台文本。 在详细信息视图中,在事件中 ListView.DrawSubItem 绘制前景文本。

有关完整示例,请参阅 DrawListViewItemEventArgs 概述参考主题。

// Draws the backgrounds for entire ListView items.
private void listView1_DrawItem(object sender,
    DrawListViewItemEventArgs e)
{
    if ((e.State & ListViewItemStates.Selected) != 0)
    {
        // Draw the background and focus rectangle for a selected item.
        e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds);
        e.DrawFocusRectangle();
    }
    else
    {
        // Draw the background for an unselected item.
        using (LinearGradientBrush brush =
            new LinearGradientBrush(e.Bounds, Color.Orange,
            Color.Maroon, LinearGradientMode.Horizontal))
        {
            e.Graphics.FillRectangle(brush, e.Bounds);
        }
    }

    // Draw the item text for views other than the Details view.
    if (listView1.View != View.Details)
    {
        e.DrawText();
    }
}
' Draws the backgrounds for entire ListView items.
Private Sub listView1_DrawItem(ByVal sender As Object, _
    ByVal e As DrawListViewItemEventArgs) _
    Handles listView1.DrawItem

    If Not (e.State And ListViewItemStates.Selected) = 0 Then

        ' Draw the background for a selected item.
        e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds)
        e.DrawFocusRectangle()

    Else

        ' Draw the background for an unselected item.
        Dim brush As New LinearGradientBrush(e.Bounds, Color.Orange, _
            Color.Maroon, LinearGradientMode.Horizontal)
        Try
            e.Graphics.FillRectangle(brush, e.Bounds)
        Finally
            brush.Dispose()
        End Try

    End If

    ' Draw the item text for views other than the Details view.
    If Not Me.listView1.View = View.Details Then
        e.DrawText()
    End If

End Sub

注解

使用此属性访问 ListViewItem 绘制。 当属性不提供足够的信息以满足你的需求时 State ,这非常有用。 该 State 属性仅提供基本状态信息,例如,用于确定是选中、选中还是聚焦项。 另一方面,该 Item 属性允许你访问该属性的所有成员 ListViewItem。 例如,必须直接访问该项,以自行绘制 ListViewItem.Text 值,而不是使用 DrawText 该方法。

适用于

另请参阅