次の方法で共有


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

次のコード例では、ListView コントロールのカスタム描画を提供するアプリケーションで Item プロパティを使用する方法を示します。 この例では、 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のすべてのメンバーにアクセスできます。 たとえば、DrawText メソッドを使用するのではなく、ListViewItem.Text値を自分で描画するには、アイテムに直接アクセスする必要があります。

適用対象

こちらもご覧ください