【问题标题】:List view Highlight selected列表视图突出显示选定
【发布时间】:2012-06-10 13:16:27
【问题描述】:

一个很简单的问题,但我认为证明它比听起来要困难得多

我想在焦点离开列表视图时保留列表视图项的选择,目前我已将 hideselection 属性设置为 false,这很好.. 它确实会导致非常浅的灰色选择在列表视图失去焦点后保持不变,所以我的问题是,如何正确显示该项目仍处于选中状态,以便用户能够识别,例如更改行文本颜色或背景颜色?还是只是在第一次选择时将其突出显示,整行变为蓝色?

我查看了 intelisense,似乎找不到任何关于行或项目或所选项目的单个颜色属性的内容?

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

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

谢谢

【问题讨论】:

标签: c# visual-studio-2010 listview


【解决方案1】:

这是一个不允许多选的 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;
    }
}

我为上面的类创建了一个类库,以便我可以将它添加到工具箱中。

【讨论】:

    【解决方案2】:

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

    How to change listview selected row backcolor even when focus on another control?

    【讨论】:

    • 给你正确答案似乎是错误的,但是这正是我想要的,哈哈!
    • 这个答案没有涵盖一些事情,但绝对应该是一个很好的起点
    猜你喜欢
    • 2015-03-19
    • 1970-01-01
    • 2013-12-22
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    • 2012-09-26
    • 1970-01-01
    相关资源
    最近更新 更多