DrawListViewItemEventArgs.State 屬性

定義

取得所要繪製 ListViewItem 的目前狀態。

C#
public System.Windows.Forms.ListViewItemStates State { get; }

屬性值

ListViewItemStates 值的位元組合,表示 ListViewItem 的目前狀態。

範例

下列程式碼範例示範如何在應用程式中使用 State 屬性,以提供控制項的 ListView 自訂繪圖。 在此範例中 ListView.DrawItem ,事件的處理常式會繪製整個專案的背景。 除了詳細資料檢視之外的所有檢視中,此處理程式也會繪製前景文字。 在詳細資料檢視中,前景文字會在 事件中 ListView.DrawSubItem 繪製。

如需完整的範例,請參閱 DrawListViewItemEventArgs 概觀參考主題。

C#
// 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();
    }
}

備註

使用這個屬性來檢查要繪製的 是否 ListViewItem 處於特定狀態。 此屬性僅提供專案的基本狀態資訊。 例如,您可以使用這個屬性來判斷專案是選取、核取還是焦點。 如果您需要深入瞭解,請透過 Item 屬性擷取專案,並直接檢查其屬性。

適用於

產品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另請參閱