简体   繁体   English

如何更改 ObjectListView 行背景色?

[英]How can I change ObjectListView row backcolor?

I have an objectlistview from BrightIdeasSoftware.我有一个来自 BrightIdeasSoftware 的对象列表视图。 Currently I can add and remove to this list however I can't paint my row colors(NOT HEADER) Simply I want to re color half of my list to red and rest to blue as example.目前我可以在这个列表中添加和删除,但是我不能绘制我的行颜色(不是标题) 简单地说,我想将我的列表的一半重新着色为红色,其余为蓝色,例如。

Normally I would do this :通常我会这样做:

            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);
            }

But this is not working, I have also tried refreshing object after re color them but still not working.但这不起作用,我也尝试在重新着色后刷新对象但仍然无法正常工作。 I have checked this but I don't want to do it with a condition I just want to give an index and then re color my listview.我已经检查过这个,但我不想在我只想给出索引然后重新着色我的列表视图的条件下进行。

Can someone show me how can I achieve this?有人可以告诉我如何实现这一目标吗? Thanks a lot非常感谢

EDIT : I will share my whole method so it will be clearer..编辑:我将分享我的整个方法,所以它会更清楚..

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: I think I might found something, I have change my method to this but it's updating itself back. 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);
            }

When I open RefreshItem it update my OLVListItem back to previous color..当我打开 RefreshItem 时,它会将我的 OLVListItem 更新回以前的颜色..

EDIT 3: I found the solution.编辑 3:我找到了解决方案。 I did Refresh() after set all my colors but now I have another problem, when I hover with my mouse the color is changing back..设置完所有颜色后,我执行了 Refresh() 但现在我遇到了另一个问题,当我用鼠标悬停时,颜色又变回了..

The documentation on their site includes a very similar example .他们网站上的文档包括一个非常相似的例子 You listen for FormatRow or FormatCell event.您侦听 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:要在客户欠款时以红色显示他们,您可以在 IDE 中为 FormatRow 事件设置一个处理程序,然后执行如下操作:

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.要更改单个单元格的格式,您需要将 UseCellFormatEvents 设置为 true,然后侦听 FormatCell 事件。 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.这些事件与 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.这些事件知道行将出现在控件中的何处,因此事件的 DisplayIndex 属性可用于更复杂的替代背景颜色方案。 The DisplayIndex is correct even when the list is showing groups and when the listview is virtual.即使列表显示组并且列表视图是虚拟的,DisplayIndex 也是正确的。

To improve performance, FormatCell events are only fired when a handler of the FormatRow event sets UseCellFormatEvents to true.为了提高性能,仅当 FormatRow 事件的处理程序将 UseCellFormatEvents 设置为 true 时才会触发 FormatCell 事件。 If you want to have a FormatCell event fired for every cell, you can set UseCellFormatEvents on the ObjectListView itself.如果您希望为每个单元格触发 FormatCell 事件,您可以在 ObjectListView 本身上设置 UseCellFormatEvents。

Okay I found the solution.好的,我找到了解决方案。 This is how I did it这就是我做到的

            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();

I call this method on FormatRow event.我在 FormatRow 事件上调用此方法。 There is 1 more thing I want to mention.还有一件事我想提一下。 This was not working until I checked UseHotControls to false.在我检查 UseHotControls 为 false 之前,这不起作用。 You know this property do some fancy things when you hover your mouse over the cell or row or whatever but I guess it's not working well with back color changes because when it was true(as default) my ObjectListView was not updating it's back color until I move my mouse over OLV or click any item but then when I was hovering and activating HotControl they were changing their color back to original(Transparent).您知道当您将鼠标悬停在单元格或行或其他任何东西上时,此属性会做一些奇特的事情,但我想它在更改背景颜色时效果不佳,因为当它为 true 时(默认情况下)我的 ObjectListView 没有更新它的背景颜色,直到我将鼠标移到 OLV 上或单击任何项​​目,但是当我悬停并激活 HotControl 时,它们将颜色改回原始(透明)。 I manage to change HotControl back color but then still I had the issue with not updating itself.我设法改变了 HotControl 的背景颜色,但我仍然遇到了不更新本身的问题。 After I set UseHotControls to false and call same method everything work perfectly.在我将 UseHotControls 设置为 false 并调用相同的方法后,一切正常。 I'll leave this method and this long paragraph here in case if someone else is going to need it.如果其他人需要它,我将把这个方法和这个长段落留在这里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM