C#winforms在数据绑定(绑定列表)后更改列表框中的选择颜色 [英] C# winforms change selection colour in a listbox in after data binding ( binding list)
本文介绍了C#winforms在数据绑定(绑定列表)后更改列表框中的选择颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
c#winforms更改选择颜色 列表框 中的数据绑定
我尝试过:
c #winforms在数据绑定后更改列表框中的选择颜色
解决方案
private void Form1_Load( object sender,EventArgs e)
{
listBox1.DataSource = new string [] { 一个, two , three};
listBox1.DrawItem + = listBox1_DrawItem;
listBox1.DrawMode = DrawMode.OwnerDrawVariable;
}
void listBox1_DrawItem( object sender,DrawItemEventArgs e)
{
if (e.Index < 0 ) return ;
// 如果选择了项目状态,则更改背景颜色
< span class =code-keyword> if ((e.State& DrawItemState.Selected)== DrawItemState.Selected)
e = new DrawItemEventArgs(e.Graphics,
e.Font,
e.Bounds,
e.Index,
e.State ^ DrawItemState.Selected,
e.ForeColor ,
Color.Yellow); // 选择颜色
// 为每个项目绘制ListBox控件的背景。
e.DrawBackground( );
// 绘制当前项目文本
e.Graphics.DrawString(listBox1 .Items [e.Index] .ToString(),e.Font,Brushes.Black,e.Bounds,StringFormat.GenericDefault);
// 如果ListBox具有焦点,则在所选项目周围绘制一个焦点矩形。
e.DrawFocusRectangle();
}
参考:更改列表框选择背景颜色 [ ^ ]
c# winforms change selection colour in a listbox in after data binding
What I have tried:
c# winforms change selection colour in a listbox in after data binding
解决方案
private void Form1_Load(object sender, EventArgs e) { listBox1.DataSource = new string[] { "one", "two", "three" }; listBox1.DrawItem += listBox1_DrawItem; listBox1.DrawMode = DrawMode.OwnerDrawVariable; } void listBox1_DrawItem(object sender, DrawItemEventArgs e) { if (e.Index < 0) return; //if the item state is selected them change the back color if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) e = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, e.State ^ DrawItemState.Selected, e.ForeColor, Color.Yellow);//Choose the color // Draw the background of the ListBox control for each item. e.DrawBackground(); // Draw the current item text e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault); // If the ListBox has focus, draw a focus rectangle around the selected item. e.DrawFocusRectangle(); }
reference : change ListBox selection background color [^]
这篇关于C#winforms在数据绑定(绑定列表)后更改列表框中的选择颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文