2014-09-19 183 views
1

我使用的是標準的.NET ListView,我想在View = LargeIcon設置中設置背景顏色。當我設置ListViewItem.BackgroundColor時,只設置文本背景。Winform ListView背景顏色LargeIcon視圖

ListView Background color

+0

你爲什麼不設置* ListView的背景色*一樣的項目嗎? – 2014-09-19 14:29:06

+0

爲整個ListView設置BackColor。我想要有白色的ListView背景和黃色矩形環繞項目。 – Marek 2014-09-19 14:34:44

+0

問題出在您的項目圖標上。它不覆蓋整個矩形。打開你的圖標圖像,並填寫所需的顏色或直接在列表視圖 – 2014-09-19 14:43:57

回答

1

我知道這是晚了幾天,但如果你仍然是一個解決方案之後,我發現一些可能的幫助。我在看到this answer後找到了解決方案。

您需要派生自己的ListView控件的版本,並添加一些自定義繪製,但這不會造成太多額外的代碼:

public class MyListView : ListView 
{ 
    public MyListView() 
     : base() 
    { 
     OwnerDraw = true; 
     DrawItem += MyListView_DrawItem; 
    } 

    private void MyListView_DrawItem(object sender, DrawListViewItemEventArgs e) 
    { 
     using (SolidBrush itemBrush = new SolidBrush(e.Item.BackColor)) 
     { 
      e.Graphics.FillRectangle(itemBrush, e.Item.Bounds); 
     } 

     e.DrawDefault = true; 
    } 
} 

我用一種新的形式對這一新的控制和我使用設計器添加了2個項目,並將項目的背景顏色設置爲「Gold」。這裏是結果:

Example of ListView item background that covers more than just the text.

+1

謝謝!這很好用。而且我甚至不需要派生List,因爲OwnerDraw和DrawItem是公開的。 – Marek 2014-09-25 07:57:41