C# ListView Detail,突出显示单个单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/215248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 18:26:57  来源:igfitidea点击:

C# ListView Detail, Highlight a single cell

c#listviewhighlightownerdrawn

提问by Mike Christiansen

I'm using a ListView in C# to make a grid. I would like to find out a way to be able to highlight a specific cell, programatically. I only need to highlight one cell.

我在 C# 中使用 ListView 来制作网格。我想找到一种能够以编程方式突出显示特定单元格的方法。我只需要突出一个单元格。

I've experimented with Owner Drawn subitems, but using the below code, I get highlighted cells, but no text! Are there any ideas on how to get this working? Thanks for your help.

我已经尝试过使用所有者绘制的子项,但是使用下面的代码,我得到了突出显示的单元格,但没有文本!有没有关于如何让这个工作的想法?谢谢你的帮助。

//m_PC.Location is the X,Y coordinates of the highlighted cell.


void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
    if ((e.ItemIndex == m_PC.Location.Y) && (e.Item.SubItems.IndexOf(e.SubItem) == m_PC.Location.X))
        e.SubItem.BackColor = Color.Blue;
    else
        e.SubItem.BackColor = Color.White;
    e.DrawBackground();
    e.DrawText();
}

采纳答案by Charlie

You can do this without owner-drawing the list:

您无需所有者绘制列表即可执行此操作:

// create a new list item with a subitem that has white text on a blue background
ListViewItem lvi = new ListViewItem( "item text" );
lvi.UseItemStyleForSubItems = false;
lvi.SubItems.Add( new ListViewItem.ListViewSubItem( lvi,
    "subitem", Color.White, Color.Blue, lvi.Font ) );

The Color arguments to the ListViewSubItem constructor are controlling the foreground and background color of the subitem. The critical thing to do here is set UseItemStyleForSubItemsto False on the list item, otherwise your color changes will be ignored.

ListViewSubItem 构造函数的 Color 参数控制子项的前景色和背景色。这里要做的关键是UseItemStyleForSubItems在列表项上设置为 False,否则您的颜色更改将被忽略。

I think your owner-draw solution would have worked as well, but you have to remember to change the text (foreground) color when you change the background to blue, otherwise the text will be hard to see.

我认为您的自绘解决方案也可以,但是您必须记住在将背景更改为蓝色时更改文本(前景色)颜色,否则文本将很难看清。

回答by Mike Christiansen

Figured it out. Here's code to toggle the highlight of a specific subitem.

弄清楚了。这是用于切换特定子项的突出显示的代码。

listView1.Items[1].UseItemStyleForSubItems = false;
if (listView1.Items[1].SubItems[10].BackColor == Color.DarkBlue)
{
    listView1.Items[1].SubItems[10].BackColor = Color.White;
    listView1.Items[1].SubItems[10].ForeColor = Color.Black;
}
else
{
    listView1.Items[1].SubItems[10].BackColor = Color.DarkBlue;
    listView1.Items[1].SubItems[10].ForeColor = Color.White;
}

回答by Chris Asquith

In my case, I wanted to highlight specific rows, including all the fields. So every row in my listview with "Medicare" in the first column gets the entire row highlighted:

就我而言,我想突出显示特定行,包括所有字段。因此,我的列表视图中第一列中带有“Medicare”的每一行都会突出显示整行:

public void HighLightListViewRows(ListView xLst)
        {
            for (int i = 0; i < xLst.Items.Count; i++)
            {
                if (xLst.Items[i].SubItems[0].Text.ToString() == "Medicare")
                {
                    for (int x = 0; x < xLst.Items[i].SubItems.Count; x++)
                    {
                        xLst.Items[i].SubItems[x].BackColor = Color.Yellow;
                    }
                }
            }
        }