Winform ListView背景颜色LargeIcon视图

问题描述:

我使用的是标准的.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

我知道这是晚了几天,但如果你仍然是一个解决方案之后,我发现一些可能的帮助。我在看到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