繁体   English   中英

列表视图已选中突出显示

[英]List view Highlight selected

很简单的问题,但我认为这将证明比听起来困难得多

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

我看过intelisense,似乎找不到行或项目或所选项目的单独颜色属性的任何内容?

但是它必须存在,因为所选项目具有自己的背景色,我在哪里可以更改呢?

哦,列表视图确实需要保留在详细信息视图中,这意味着我无法使用谷歌搜索时只能找到的唯一方法

谢谢

这是ListView的解决方案,该解决方案不允许多项选择并且没有图像(例如复选框)。

  1. 设置ListView的事件处理程序(在此示例中,其名为listView1 ):
    • DRAWITEM
    • 离开(当失去ListView的焦点时调用)
  2. 声明一个全局int变量(即,包含ListView的Form的成员,在此示例中,其名为gListView1LostFocusItem )并为其分配值-1
    • int gListView1LostFocusItem = -1;
  3. 实现事件处理程序,如下所示:

     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控件,因此您可以将受保护的属性DoubleBuffered更改为true。

public class ListViewEx : ListView
{
    public ListViewEx() : base()
    {
        this.DoubleBuffered = true;
    }
}

我创建了上述类的类库,以便可以将其添加到工具箱中。

一个可能的解决方案可能是对另一个问题的答案:

即使将焦点放在另一个控件上,如何更改列表视图所选行的背景色?

暂无
暂无

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

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