C# 列表视图突出显示所选
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10915643/
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
List view Highlight selected
提问by Alex
Quite a simple question but I think its going to prove much harder than it sounds
一个很简单的问题,但我认为它会比听起来要困难得多
I'd like to keep the selection of a listview item there when the focus leaves the list view, at the moment I've set the hideselectionproperty to false and that's fine.. it does cause a VERY light gray selection to stay after the list view loses focus, so my question is, how can I properly show that that item is still selected so that the user will recognize that, something like changing the rows text colour or background colour? or just keeping it highlighted as when first selected the whole row turns blue?
当焦点离开列表视图时,我想在那里保留对列表视图项的选择,目前我已将该hideselection属性设置为 false,这很好.. 它确实导致非常浅灰色的选择留在列表之后视图失去焦点,所以我的问题是,如何正确显示该项目仍处于选中状态,以便用户能够识别,例如更改行文本颜色或背景颜色?或者只是在第一次选择时保持突出显示整行变成蓝色?
I've had a look through intelisense and can't seem to find anything for a row or item or selected item's individual colour property?
我已经查看了 intelisense 并且似乎无法找到行或项目或所选项目的单个颜色属性的任何内容?
It must exist though because selected items have their own background colour, where would I be able to change that?
它必须存在,因为选定的项目有自己的背景颜色,我可以在哪里更改它?
Oh and the list view does need to stay in details view, which means I can't use the only method that I've been able to find whilst googling
哦,列表视图确实需要保留在详细信息视图中,这意味着我无法使用在谷歌搜索时能够找到的唯一方法
thanks
谢谢
回答by Zsolt Molnar
A possible solution might be this answer to an another question:
一个可能的解决方案可能是另一个问题的答案:
How to change listview selected row backcolor even when focus on another control?
回答by user2232952
Here's a solution for a ListView that does not allow multiple selections and does not have images (e.g. checkboxes).
这是一个 ListView 的解决方案,它不允许多选且没有图像(例如复选框)。
- Set event handlers for the ListView (in this example it's named listView1):
- DrawItem
- Leave (invoked when the ListView's focus is lost)
- Declare a global int variable (i.e. a member of the Form that contains the ListView,
in this example it's named gListView1LostFocusItem) and assign it the value -1
- int gListView1LostFocusItem = -1;
Implement the event handlers as follows:
private void listView1_Leave(object sender, EventArgs e) { // Set the global int variable (gListView1LostFocusItem) to // the index of the selected item that just lost focus gListView1LostFocusItem = listView1.FocusedItem.Index; } private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e) { // If this item is the selected item if (e.Item.Selected) { // If the selected item just lost the focus if (gListView1LostFocusItem == e.Item.Index) { // Set the colors to whatever you want (I would suggest // something less intense than the colors used for the // selected item when it has focus) e.Item.ForeColor = Color.Black; e.Item.BackColor = Color.LightBlue; // Indicate that this action does not need to be performed // again (until the next time the selected item loses focus) gListView1LostFocusItem = -1; } else if (listView1.Focused) // If the selected item has focus { // Set the colors to the normal colors for a selected item e.Item.ForeColor = SystemColors.HighlightText; e.Item.BackColor = SystemColors.Highlight; } } else { // Set the normal colors for items that are not selected e.Item.ForeColor = listView1.ForeColor; e.Item.BackColor = listView1.BackColor; } e.DrawBackground(); e.DrawText(); }
- 为 ListView 设置事件处理程序(在这个例子中它被命名为listView1):
- 绘制项
- 离开(当 ListView 的焦点丢失时调用)
- 声明一个全局 int 变量(即包含 ListView 的 Form 的成员,在本例中它被命名为gListView1LostFocusItem)并为其分配值 -1
- int gListView1LostFocusItem = -1;
实现事件处理程序如下:
private void listView1_Leave(object sender, EventArgs e) { // Set the global int variable (gListView1LostFocusItem) to // the index of the selected item that just lost focus gListView1LostFocusItem = listView1.FocusedItem.Index; } private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e) { // If this item is the selected item if (e.Item.Selected) { // If the selected item just lost the focus if (gListView1LostFocusItem == e.Item.Index) { // Set the colors to whatever you want (I would suggest // something less intense than the colors used for the // selected item when it has focus) e.Item.ForeColor = Color.Black; e.Item.BackColor = Color.LightBlue; // Indicate that this action does not need to be performed // again (until the next time the selected item loses focus) gListView1LostFocusItem = -1; } else if (listView1.Focused) // If the selected item has focus { // Set the colors to the normal colors for a selected item e.Item.ForeColor = SystemColors.HighlightText; e.Item.BackColor = SystemColors.Highlight; } } else { // Set the normal colors for items that are not selected e.Item.ForeColor = listView1.ForeColor; e.Item.BackColor = listView1.BackColor; } e.DrawBackground(); e.DrawText(); }
Note: This solution can result in some flicker. A fix for this involves subclassing the ListView control so you can change the protected property DoubleBufferedto true.
注意:此解决方案可能会导致一些闪烁。对此的修复涉及对 ListView 控件进行子类化,以便您可以将受保护的属性DoubleBuffered更改为 true。
public class ListViewEx : ListView
{
public ListViewEx() : base()
{
this.DoubleBuffered = true;
}
}
I created a class library of the above class so that I could add it to the toolbox.
我创建了上述类的类库,以便将其添加到工具箱中。

