开发者

Show subitems in C# ListView when using Tile view

I'm using ListView component to display some saved profiles in my program, each profile has an icon and a title. So I used, the ListView with View property set to Tile (also groupping was applied)

How I wan't to add some description text about the entries (not a tooltip).开发者_开发百科 Can I do it like Windows Explorer does?

Show subitems in C# ListView when using Tile view


You probably need to define column headers. From the documentation:

The tile view displays each item with a large icon on the left and textual information on the right. The textual information consists of the item label followed by subitems. By default, only the first subitem is displayed, which corresponds to the item label. To display additional subitems, you must add ColumnHeader objects to the Columns collection. Each subitem in the tile corresponds to a column header. To control which subitems are displayed and the order in which they are displayed, you must set the ListViewItem.ListViewSubItem.Name property for each item and the ColumnHeader.Name property for each header. You can then add, remove, and rearrange headers in the Columns collection to achieve the desired result.


I'm not sure if this will do exactly what you want, since I've only tested it without any kind of icon, but it will write the subitems when the ListView is in Tile mode.

First set OwnerDraw on your ListView to true and then add this function to its DrawItem event:

private void statisticsView_DrawItem(object sender,
    DrawListViewItemEventArgs e)
{
    e.Graphics.FillRectangle(Brushes.White, e.Bounds);
    if((e.State & ListViewItemStates.Selected) != 0)
        e.DrawFocusRectangle();

    Rectangle bounds = e.Bounds;
    Font font = ((ListView)sender).Font;
    Font nameFont = new Font(font.FontFamily,
        font.Size,
        FontStyle.Underline | FontStyle.Bold,
        font.Unit,
        font.GdiCharSet,
        font.GdiVerticalFont);

    using(StringFormat sf = new StringFormat())
    {
        e.Graphics.DrawString(e.Item.SubItems[0].Text,
            nameFont, Brushes.Black, bounds, sf);
        bounds.Y += 17;
        for(int i = 1; i < e.Item.SubItems.Count; ++i, bounds.Y += 15)
            e.Graphics.DrawString(e.Item.SubItems[i].Text,
                font, Brushes.Black, bounds, sf);
    }
}

First it fills the tile with a white background colour and if the tile is selected it draws a selection rectangle around it. The next part just takes the font set on the ListView and creates a duplicate font with a few changes. Lastly it draws each of the subitems while moving the drawing rectangle down 15 pixels after each subitem. The first subitem is drawn with the separate font and is 17 pixels above the rest.

You should be able to modify the code to suit your needs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜