DrawListViewSubItemEventArgs.ItemState 属性

定义

获取要绘制的 ListViewItem 的父级 ListViewItem.ListViewSubItem 的当前状态。

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

属性值

ListViewItemStates 值的按位组合,指示父级 ListViewItem 的当前状态。

示例

下面的代码示例演示如何在为控件提供自定义绘图ListView的应用程序中使用 ItemState 属性。 在此示例中,事件的处理程序 ListView.DrawSubItem 绘制子项文本值,以及具有负值的子项的文本和背景。

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

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

注解

使用此属性可检查要绘制的 父级ListViewItem.ListViewSubItem是否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

另请参阅