2012-06-06 68 views
1

一个非常简单的问题,但我认为它会证明比它听起来更难列表视图高亮选择

我想保持一个ListView项那里的选择,当焦点离开列表视图中,在我已经将hideselection属性设置为false,这很好..它确实会导致非常浅灰色的选择留在列表视图失去焦点后,所以我的问题是,我该如何正确显示该项目仍然选择,以便用户会认识到,像改变行的文字颜色或背景颜色?或只是保持突出显示,当第一次选择整行变成蓝色?

我已经看过intelisense,似乎无法找到任何行或项目或选定项目的个别颜色属性?

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

哦,列表视图也需要留在细节来看,这意味着我不能使用,我已经能够找到,而谷歌搜索

感谢

+0

[如何改变列表视图选择行backcolor甚至当焦点在另一个控制?](http:// stackoverflow。COM /问题/ 5179664 /如何对变化的ListView选择-行背景色偶数时,聚焦启动 - 另一个控制) –

+0

是啊,我被判MODS的问题,因为我还是新的网站和不确定做什么 – Alex

+0

太广,你有相应的技术来对其进行标记(如的WinForms,WPF等)。 – Sinatr

回答

4

下面是一个ListView不允许多重选择和 没有图像(例如复选框)的解决方案。

  1. 组事件处理程序为ListView(在这个例子中它的命名ListView1的):
    • DRAWITEM
    • 离开(当ListView的焦点丢失调用)
  2. 声明一个全局变量int(即包含ListView的表单的成员,在本例中为 ,其名称为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; 
    } 
} 

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