2012-06-06 66 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; 
    } 
} 

我創建了上述類的類庫,以便我可以將它添加到工具箱中。