【问题标题】:How can I change ObjectListView row backcolor?如何更改 ObjectListView 行背景色?
【发布时间】:2019-04-05 08:55:29
【问题描述】:

我有一个来自 BrightIdeasSoftware 的 objectlistview。目前我可以在这个列表中添加和删除,但是我不能绘制我的行颜色(NOT 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);
            }

但这不起作用,我也尝试在重新着色后刷新对象但仍然不起作用。我已经检查了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() 但现在我遇到了另一个问题,当我用鼠标悬停时,颜色又变回来了..

【问题讨论】:

  • 请阅读此解决方案,它可能对您有所帮助:stackoverflow.com/questions/13087648/…
  • 这是使用普通的 ListView。答案是正确的,如果我也使用 Windows ListView 它将起作用。但相反,我使用 ObjectListView,这不起作用
  • 作为使用 OLV 的一般建议,请务必阅读 this。如果您发现自己在用 Items[] stop 搞乱,几乎肯定有更好的方法。
  • 感谢您的链接,在 OLV 中不再使用 Items[] :)
  • 一如既往:webforms 还是 winforms?

标签: c# objectlistview


【解决方案1】:

他们网站上的文档包括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 也是正确的。

为了提高性能,仅当 FormatRow 事件的处理程序将 UseCellFormatEvents 设置为 true 时才会触发 FormatCell 事件。如果您想为每个单元格触发 FormatCell 事件,您可以在 ObjectListView 本身上设置 UseCellFormatEvents。

【讨论】:

  • 我已经阅读了他们指南上的这一部分,但我不需要“听”我必须在需要时进行设置。基本上我需要像我在我的问题上发布的东西。一种将索引并将列表视图绘制为特定颜色直到索引然后将其余颜色绘制为另一种颜色的方法。编辑:有没有办法手动触发这个事件?
【解决方案2】:

好的,我找到了解决方案。 我就是这样做的

            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 并调用相同的方法后,一切正常。如果其他人需要它,我将把这个方法和这一长段留在这里。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 2012-02-15
    • 1970-01-01
    • 2016-03-15
    • 2011-11-11
    相关资源
    最近更新 更多