ListView.DrawSubItem 事件

定义

当绘制 ListView 的详细信息视图并且 OwnerDraw 属性设置为 true 时发生。

C#
public event System.Windows.Forms.DrawListViewSubItemEventHandler DrawSubItem;
C#
public event System.Windows.Forms.DrawListViewSubItemEventHandler? DrawSubItem;

事件类型

示例

下面的代码示例提供了 事件处理程序的 DrawSubItem 实现。 有关完整示例,请参阅 OwnerDraw 参考主题。

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);
    }
}

注解

此事件允许你使用所有者绘图自定义控件的外观 ListView 。 仅当 属性设置为 且 属性设置为 View.DetailstrueOwnerDrawView才会引发该属性。 有关所有者绘图的详细信息,请参阅 OwnerDraw 属性参考主题。

备注

子项信息通常显示在磁贴视图和详细信息视图中,但在磁贴视图中,必须在事件的处理程序 DrawItem 中绘制子项信息。

每个DrawSubItemListView子项都可能发生 该事件。 可以处理 DrawItem 事件以绘制所有子项(如背景)共有的元素,并处理 DrawSubItem 事件以绘制单个子项(如文本值)的元素。 还可以仅使用两个事件中的一个来绘制控件中的所有 ListView 项,尽管这可能不太方便。 若要在详细信息视图中绘制列标题,必须处理 事件 DrawColumnHeader

备注

对于DrawSubItem未向集合中添加Columns任何ColumnHeader对象的子项,不会发生 该事件。 另请注意,每个 ListViewItem 对象的第一个子项表示父项本身,并显示在第一列中。

有关处理事件的详细信息,请参阅 处理和引发事件

适用于

产品 版本
.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

另请参阅