开发者

Inconsistent listview subitem properties

I'm confused here. I am trying to stylize various cells of this listview but the results are odd. my code to create the listviewitem is as follows:

lvi = new ListViewItem();
lvi.Text = row["Size"].ToString();
lvi.SubItems.Add(row["Item"].ToString());
lvi.SubItems[0].Font = new System.Drawing.Font(lvi.Font, FontStyle.Underline);
lvi.SubItems[1].ForeColor = Color.Blue;
listAvailableSizes.Items.Add(lvi);

So, in theory my first subitem should be underlined and my second one should be blue (these are just arbitrary styles). And the immediate window confirms that this should be the case:

    listAvailableSizes.Items[0].SubItems[0].ForeColor
    "{Name=WindowText, ARGB=(255, 0, 0, 0)}"
        A: 255
        B: 0
        G: 0
        IsEmpty: false
        IsKnownColor: true
        IsNamedColor: t开发者_JS百科rue
        IsSystemColor: true
        Name: "WindowText"
        R: 0
    listAvailableSizes.Items[0].SubItems[1].ForeColor
    "{Name=Blue, ARGB=(255, 0, 0, 255)}"
        A: 255
        B: 255
        G: 0
        IsEmpty: false
        IsKnownColor: true
        IsNamedColor: true
        IsSystemColor: false
        Name: "Blue"
        R: 0
    listAvailableSizes.Items[0].SubItems[0].Font.Underline
    true
    listAvailableSizes.Items[0].SubItems[1].Font.Underline
    false

According to this, subitem position 0 has regular colored text and an underline, position 1 has blue text and no underline, however, this is how it displays:

Inconsistent listview subitem properties

As you can see both are underlined and neither is blue. Am I missing something?


Normally, all subitems have the same styling (font and colors) as the item. (FYI, SubItem[0] is the same thing as the ListViewItem itself).

To allow subitems to have different attributes, do this:

listAvailableSizes.Items[0].UseItemStyleForSubItems = false; 

Once this is set to false, the ListView will actually look at the values in the subitems when deciding what font/color to use.

If UseItemStyleForSubItems is true, you can set the styles in the subitems, and they are stored OK -- it's just that they will never be used.


Subitems do not support colorization using the standard .NET Listview implementation. You need to look into custom drawing your listview.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜