我有BrightIdeasSoftware的objectlistview。目前我可以添加和删除到这个列表但是我不能绘制我的行颜色(不是标题)我只想将我的列表的一半颜色变为红色并以蓝色为例。
通常我会这样做:
for (int i = 0; i < index; i++)
{
mainForm.MyListView.Items[i].BackColor = Color.LightGray;
}
mainForm.MyListView.Items[index].BackColor = Color.DarkGreen;
for (int i = index; i < mainForm.MyListView.Items.Count; i++)
{
mainForm.MyListView.Items[i].BackColor = Color.FromArgb(18, 18, 18);
}
但这不起作用,我也尝试刷新对象后重新着色但仍然无法正常工作。我已经检查了this,但我不想这样做,我只想给一个索引,然后重新着色我的listview。
有人可以告诉我如何实现这一目标?非常感谢
编辑:我将分享我的整个方法,以便更清楚..
public void PaintToIndex(int index)
{
for (int i = 0; i < index; i++)
{
mainForm.MyListView.Items[i].BackColor = Color.LightGray;
}
mainForm.MyListView.Items[index].BackColor = Color.DarkGreen;
for (int i = index; i < mainForm.MyListView.Items.Count; i++)
{
mainForm.MyListView.Items[i].BackColor = Color.FromArgb(18, 18, 18);
}
}
编辑2:我想我可能会找到一些东西,我已经改变了我的方法,但它正在更新自己。
for (int i = 0; i < index; i++)
{
OLVListItem CurItem = mainForm.MyListView.GetItem(i);
CurItem.BackColor = Color.LightGray;
//mainForm.MyListView.RefreshItem(CurItem);
}
mainForm.MyListView.GetItem(index).BackColor = Color.LightGray;
for (int i = index; i < mainForm.MyListView.Items.Count; i++)
{
OLVListItem CurItem = mainForm.MyListView.GetItem(i);
CurItem.BackColor = Color.FromArgb(18, 18, 18);
//mainForm.MyListView.RefreshItem(CurItem);
}
当我打开RefreshItem时,它将我的OLVListItem更新回以前的颜色..
编辑3:我找到了解决方案。在设置了所有颜色之后我做了Refresh()但是现在我有另一个问题,当我用鼠标悬停时颜色正在改变。
他们网站上的文档包括very similar example。您监听FormatRow或FormatCell事件。
要在客户欠钱时向客户显示红色,您可以在IDE中为FormatRow事件设置处理程序,然后执行以下操作:
private void olv1_FormatRow(object sender, FormatRowEventArgs e) {
Customer customer = (Customer)e.Model;
if (customer.Credit < 0)
e.Item.BackColor = Color.Red;
}
要更改单个单元格的格式,需要将UseCellFormatEvents设置为true,然后侦听FormatCell事件。要仅显示红色的贷方余额,您可以执行以下操作:
private void olv1_FormatCell(object sender, FormatCellEventArgs e) {
if (e.ColumnIndex == this.creditBalanceColumn.Index) {
Customer customer = (Customer)e.Model;
if (customer.Credit < 0)
e.SubItem.ForeColor = Color.Red;
}
}
这些事件与UseAlternatingBackColors配合得很好。您在这些事件中执行的任何格式优先于备用背景颜色。
这些事件知道行将在控件中出现的位置,因此事件的DisplayIndex属性可用于更复杂的备用背景颜色方案。即使列表显示组并且列表视图是虚拟的,DisplayIndex也是正确的。
为了提高性能,FormatCell事件仅在FormatRow事件的处理程序将UseCellFormatEvents设置为true时触发。如果要为每个单元格触发FormatCell事件,可以在ObjectListView本身上设置UseCellFormatEvents。
好的,我找到了解决方案。这就是我做到的
int CurrentIndex = StaticVariables.MyListView.GetPlaylistCurrentIndex();
int count = StaticVariables.MyListView.GetPlaylistCount();
for (int i = 0; i < CurrentIndex; i++)
{
OLVListItem item = mainForm.MyListView.GetItem(i);
item.BackColor = Color.FromArgb(35, 35, 35);
}
for (int i = CurrentIndex; i < count; i++)
{
OLVListItem item = mainForm.MyListView.GetItem(i);
item.BackColor = Color.FromArgb(18, 18, 18);
}
OLVListItem item2 = mainForm.MyListView.GetItem(CurrentIndex);
item2.BackColor = Color.DarkGreen;
mainForm.MyListView.Refresh();
我在FormatRow事件上调用此方法。我还想提一件事。在我将UseHotControls检查为false之前,这不起作用。当你将鼠标悬停在单元格或行或其他任何东西上时,你知道这个属性做了一些奇特的事情,但我猜它不适合背面颜色变化,因为当它是真的(默认情况下)我的ObjectListView没有更新它的背面颜色,直到我将我的鼠标移动到OLV上或单击任何项目,但当我在盘旋并激活HotControl时,他们将其颜色更改回原始(透明)。我设法改变HotControl的颜色,但后来我仍然遇到了没有自我更新的问题。在我将UseHotControls设置为false并调用相同的方法之后,一切都运行良好。如果其他人需要它,我会留下这个方法和这个长段落。