Moh*_*yan 3 c# listview background-color
我要为ListView行设置备用颜色.
我看到了这个链接,但我使用的是.Net Framework 3.5 SP1,所以我无法使用它.
我使用了以下代码,但它有ListView排序问题
ListViewItem newListViewItem = new ListViewItem(
new string[] { item.name.ToString(),
item.code.ToString() });
newListViewItem.BackColor = new Color(240,240,240);
newListViewItem.UseItemStyleForSubItems = true;
newListViewItem.Font = new Font("Tahoma", 9);
listView1.Items.Add(newListViewItem);
Run Code Online (Sandbox Code Playgroud)
你能指导我怎么做吗?
你的片段很不清楚,但我猜你想要交替颜色.偶数编号的项目以单向着色,奇数编号以另一种方式着色.是的,当视图中有大量列时,作为阅读指南非常有效.
是的,当你对物品进行分类时,这会变得混乱.排序后,你需要一个简单的for循环来改变BackColor属性.
private static void recolorListItems(ListView lv) {
for (int ix = 0; ix < lv.Items.Count; ++ix) {
var item = lv.Items[ix];
item.BackColor = (ix % 2 == 0) ? Color.Beige : Color.White;
}
}
Run Code Online (Sandbox Code Playgroud)
排序后调用此方法.或者在填充ListView之后.我吮吸颜色,请自己选择.