如何更改 ObjectListView 行背景色?
How can I change ObjectListView row backcolor?
我有一个来自 BrightIdeasSoftware 的 objectlistview。目前我可以添加和删除到这个列表,但是我不能绘制我的行颜色(不是 HEADER)只是我想将我的列表的一半重新着色为红色,其余部分为蓝色。
通常我会这样做:
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);
}
但这不起作用,我也尝试在重新着色后刷新 object 但仍然不起作用。我已经检查了 this 但我不想在我只想提供索引然后重新着色我的列表视图的条件下执行此操作。
有人可以告诉我如何实现吗?非常感谢
编辑:我将分享我的整个方法,这样它会更清楚..
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);
}
}
EDIT2:我想我可能找到了一些东西,我已经改变了我的方法,但它正在自我更新。
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 事件。
To show customers in red when they owe money, you would set up a handler for the FormatRow event in the IDE, and then do something like this:
private void olv1_FormatRow(object sender, FormatRowEventArgs e) {
Customer customer = (Customer)e.Model;
if (customer.Credit < 0)
e.Item.BackColor = Color.Red;
}
To change the formatting of an individual cell, you need to set UseCellFormatEvents to true and then listen for FormatCell events. To show just the credit balance in red, you could do something like this:
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;
}
}
These events play well with UseAlternatingBackColors. Any formatting you do in these events takes precedence over the alternate back colours.
These events know where the row is going to appear in the control, so the DisplayIndex property of the event can be used for more sophisticated alternate background colour schemes. The DisplayIndex is correct even when the list is showing groups and when the listview is virtual.
To improve performance, FormatCell events are only fired when a handler of the FormatRow event sets UseCellFormatEvents to true. If you want to have a FormatCell event fired for every cell, you can set UseCellFormatEvents on the ObjectListView itself.
好的,我找到了解决方案。
我就是这样做的
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 并调用相同的方法后,一切正常。我将把这个方法和这个长段留在这里,以防其他人需要它。
我有一个来自 BrightIdeasSoftware 的 objectlistview。目前我可以添加和删除到这个列表,但是我不能绘制我的行颜色(不是 HEADER)只是我想将我的列表的一半重新着色为红色,其余部分为蓝色。
通常我会这样做:
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);
}
但这不起作用,我也尝试在重新着色后刷新 object 但仍然不起作用。我已经检查了 this 但我不想在我只想提供索引然后重新着色我的列表视图的条件下执行此操作。
有人可以告诉我如何实现吗?非常感谢
编辑:我将分享我的整个方法,这样它会更清楚..
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);
}
}
EDIT2:我想我可能找到了一些东西,我已经改变了我的方法,但它正在自我更新。
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 事件。
To show customers in red when they owe money, you would set up a handler for the FormatRow event in the IDE, and then do something like this:
private void olv1_FormatRow(object sender, FormatRowEventArgs e) {
Customer customer = (Customer)e.Model;
if (customer.Credit < 0)
e.Item.BackColor = Color.Red;
}
To change the formatting of an individual cell, you need to set UseCellFormatEvents to true and then listen for FormatCell events. To show just the credit balance in red, you could do something like this:
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;
}
}
These events play well with UseAlternatingBackColors. Any formatting you do in these events takes precedence over the alternate back colours.
These events know where the row is going to appear in the control, so the DisplayIndex property of the event can be used for more sophisticated alternate background colour schemes. The DisplayIndex is correct even when the list is showing groups and when the listview is virtual.
To improve performance, FormatCell events are only fired when a handler of the FormatRow event sets UseCellFormatEvents to true. If you want to have a FormatCell event fired for every cell, you can set UseCellFormatEvents on the ObjectListView itself.
好的,我找到了解决方案。 我就是这样做的
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 并调用相同的方法后,一切正常。我将把这个方法和这个长段留在这里,以防其他人需要它。