试试这个:(请记住,这会给你一个开始。有更好的方法来实现它。
enum Level
{
Warning,
Error
}
class ITemInListView
{
public string message { get; set; }
public Level level { get; set; }
}
将您的 ListView 属性设置为:
- 添加一个具有空 Text 属性的列。
- 将 View 属性设置为 Details
- 将 HeaderStyle 属性设置为无。
然后将其用作(仅作为示例):
var list = new List<ITemInListView>();
var item1 = new ITemInListView()
{
message = "This is item1",
level = Level.Warning
};
var item2 = new ITemInListView()
{
message = "This is item2",
level = Level.Error
};
var item3 = new ITemInListView()
{
message = "This is item3",
level = Level.Error
};
var item4 = new ITemInListView()
{
message = "This is item4",
level = Level.Warning
};
list.Add(item1);
list.Add(item2);
list.Add(item3);
list.Add(item4);
//Set the width of the column to be the width of the ListView control so it expands to full size.
lvMessages.Columns[0].Width = lvMessages.Width;
foreach (ITemInListView item in list)
{
var lvi = lvMessages.Items.Add(item.message);
switch (item.level)
{
case Level.Warning:
lvi.BackColor = Color.Yellow;
break;
case Level.Error:
lvi.BackColor = Color.Red;
break;
}
}