在ListView的C#(.NET 3.5)替代颜色? [英] Alternate Color in ListView C# (.Net 3.5)?
问题描述
我要设置一个备用的色彩我的ListView行。
我看到此链接但我使用.NET Framework 3.5 SP1男,所以我不能使用它。
我用下面的code,但它有问题的ListView排序
I'm gonna set an alternate color to my ListView rows.
I saw this link but I'm using .Net Framework 3.5 SP1 , so I can't use it.
I've used the following code , but it has problem with ListView Sorting
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);
可否请你指导我我该怎么办呢?
Could you please guide me how I can do it?
推荐答案
这是从你的片段还不是很清楚,但我会想你想改变颜色。偶数项目有色的一种方式,奇数有色另一种方式。是的,作为一个导读非常有效的,当你在视图中的大量列。
It is quite unclear from your snippet, but I'll guess you want alternating colors. Even numbered items colored one way, odd numbered colored another way. Yes, very effective as a reading guide when you have a large number of columns in the view.
是的,这是怎么回事,当你排序的项目得到打乱了。排序之后,你需要的for循环的简单改变背景色属性。
And yes, that's going to get mucked up when you sort the items. Right after sorting, you'll need a simple for loop that changes the BackColor property.
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;
}
}
排序后调用此。或填充ListView控件之后。我吸在颜色,请选择你自己的。
Call this after sorting. Or after filling the ListView. I suck at colors, please pick your own.
这篇关于在ListView的C#(.NET 3.5)替代颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!