【问题标题】:how to add color to sub item in listview? c#如何为列表视图中的子项目添加颜色? C#
【发布时间】:2017-03-26 13:58:21
【问题描述】:

我尝试通过方法从数组中获取的索引将文本颜色添加到列表视图中的任何子项

for (int i = 0; i < sizes.Length; ++i)
            {
                if (sizes[i] == 1)
                {
                    Item.SubItems.Add("In Stock");
                }
                else if (sizes[i] == 0)
                {
                    Item.SubItems.Add("Out Of Stock");
                }
                else if (sizes[i] == 2)
                {
                    Item.SubItems.Add("Less Than 3");
                }
                else if (sizes[i] == 5)
                {
                    Item.SubItems.Add("Less Than 5");
                }
                else if (sizes[i] == 10)
                {
                    Item.SubItems.Add("Less Than 10");
                }
            }

            ProductListView.Items.Add(Item);
        }

如果有库存尺寸,子项目颜色变为绿色 如果尺码缺货,子项颜色将变为红色

谢谢

【问题讨论】:

    标签: c# listview subitem


    【解决方案1】:

    Add 方法返回子项,
    所以你可以像这样改变 subItem 颜色:

    var subItem = Item.SubItems.Add("In Stock");
    subItem.ForeColor = Color.Green;
    // subItem.BackColor = Color.Red;
    

    【讨论】:

    • 不要忘记为每个项目关闭UseItemStyleForSubItems
    • 感谢itemUser.UseItemStyleForSubItems = false; 选项!颜色仍然可以像这样分配itemUser.SubItems.Add("Text", System.Drawing.Color.Empty, System.Drawing.Color.Blue, null);
    猜你喜欢
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多