ListBox项目的背景颜色(winforms)
如何设置System.Windows.Forms.ListBox中特定项目的背景颜色? 我希望能够设置多个,如果可能的话。
可能唯一的办法就是自己画画。
将DrawMode
设置为OwnerDrawFixed
并在DrawItem事件上编写这样的代码:
private void listBox_DrawItem(object sender, DrawItemEventArgs e) { e.DrawBackground(); Graphics g = e.Graphics; g.FillRectangle(new SolidBrush(Color.Silver), e.Bounds); // Print text e.DrawFocusRectangle(); }
第二种select是使用ListView,虽然他们有其他方式的实现(不是真正的数据绑定,但更灵活的列方式)
感谢Grad van Horck的回答 ,它引导我朝着正确的方向前进。
为了支持文本(不只是背景颜色),这里是我完全工作的代码:
//global brushes with ordinary/selected colors private SolidBrush reportsForegroundBrushSelected = new SolidBrush(Color.White); private SolidBrush reportsForegroundBrush = new SolidBrush(Color.Black); private SolidBrush reportsBackgroundBrushSelected = new SolidBrush(Color.FromKnownColor(KnownColor.Highlight)); private SolidBrush reportsBackgroundBrush1 = new SolidBrush(Color.White); private SolidBrush reportsBackgroundBrush2 = new SolidBrush(Color.Gray); //custom method to draw the items, don't forget to set DrawMode of the ListBox to OwnerDrawFixed private void lbReports_DrawItem(object sender, DrawItemEventArgs e) { e.DrawBackground(); bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected); int index = e.Index; if (index >= 0 && index < lbReports.Items.Count) { string text = lbReports.Items[index].ToString(); Graphics g = e.Graphics; //background: SolidBrush backgroundBrush; if (selected) backgroundBrush = reportsBackgroundBrushSelected; else if ((index % 2) == 0) backgroundBrush = reportsBackgroundBrush1; else backgroundBrush = reportsBackgroundBrush2; g.FillRectangle(backgroundBrush, e.Bounds); //text: SolidBrush foregroundBrush = (selected) ? reportsForegroundBrushSelected : reportsForegroundBrush; g.DrawString(text, e.Font, foregroundBrush, lbReports.GetItemRectangle(index).Location); } e.DrawFocusRectangle(); }
上面的代码会添加到给定的代码中,并显示正确的文本和高亮选中的项目。
// Set the background to a predefined colour MyListBox.BackColor = Color.Red; // OR: Set parts of a color. MyListBox.BackColor.R = 255; MyListBox.BackColor.G = 0; MyListBox.BackColor.B = 0;
如果你的意思是通过设置多个背景颜色来为每个项目设置不同的背景颜色,那么这对于一个ListBox是不可能的,而对于一个ListView则是不可能的,如下所示:
// Set the background of the first item in the list MyListView.Items[0].BackColor = Color.Red;
public Picker() { InitializeComponent(); this.listBox.DrawMode = DrawMode.OwnerDrawVariable; this.listBox.MeasureItem += listBoxMetals_MeasureItem; this.listBox.DrawItem += listBoxMetals_DrawItem; } void listBoxMetals_DrawItem(object sender, DrawItemEventArgs e) { e.DrawBackground(); Brush myBrush = Brushes.Black; var item = listBox.Items[e.Index] as Mapping; if (e.Index % 2 == 0) { e.Graphics.FillRectangle(new SolidBrush(Color.GhostWhite), e.Bounds); } e.Graphics.DrawString(item.Name, e.Font, myBrush, e.Bounds, StringFormat.GenericDefault); e.DrawFocusRectangle(); }
完整的样本
private void listbox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { e.DrawBackground(); Brush myBrush = Brushes.Black; var item = listbox1.Items[e.Index]; if(e.Index % 2 == 0) { e.Graphics.FillRectangle(new SolidBrush(Color.Gold), e.Bounds); } e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), e.Font, myBrush,e.Bounds,StringFormat.GenericDefault); e.DrawFocusRectangle(); } public MainForm() { InitializeComponent(); this.listbox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listbox1_DrawItem); }