简体   繁体   English

列表视图已选中突出显示

[英]List view Highlight selected

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 hideselection property 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? 当焦点离开列表视图时,我想将列表视图项的选择保留在那里,此刻,我将hideselection属性设置为false,这很好。.确实会导致非常浅的灰色选择保留在列表视图失去了焦点,所以我的问题是,如何正确显示仍处于选中状态的项目,以便用户识别出来,例如更改行的文本颜色或背景颜色? or just keeping it highlighted as when first selected the whole row turns blue? 还是只是在第一次选择时将其突出显示,否则整个行都会变成蓝色?

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 谢谢

Here's a solution for a ListView that does not allow multiple selections and does not have images (eg checkboxes). 这是ListView的解决方案,该解决方案不允许多项选择并且没有图像(例如复选框)。

  1. Set event handlers for the ListView (in this example it's named listView1 ): 设置ListView的事件处理程序(在此示例中,其名为listView1 ):
    • DrawItem DRAWITEM
    • Leave (invoked when the ListView's focus is lost) 离开(当失去ListView的焦点时调用)
  2. Declare a global int variable (ie a member of the Form that contains the ListView, in this example it's named gListView1LostFocusItem ) and assign it the value -1 声明一个全局int变量(即,包含ListView的Form的成员,在此示例中,其名为gListView1LostFocusItem )并为其分配值-1
    • int gListView1LostFocusItem = -1; int gListView1LostFocusItem = -1;
  3. 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(); } 

Note: This solution can result in some flicker. 注意:此解决方案可能会导致闪烁。 A fix for this involves subclassing the ListView control so you can change the protected property DoubleBuffered to 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. 我创建了上述类的类库,以便可以将其添加到工具箱中。

A possible solution might be this answer to an another question: 一个可能的解决方案可能是对另一个问题的答案:

How to change listview selected row backcolor even when focus on another control? 即使将焦点放在另一个控件上,如何更改列表视图所选行的背景色?

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

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