0
我在我的windows应用程序中有一个组合框,它是DataBound到一个只读列表。我的要求是显示一些基于列表属性的粗体项目。该属性与值成员和显示成员的属性不同。无论如何没有循环通过每个项目,因为列表太大了?在组合框中显示一些项目以粗体显示
我在我的windows应用程序中有一个组合框,它是DataBound到一个只读列表。我的要求是显示一些基于列表属性的粗体项目。该属性与值成员和显示成员的属性不同。无论如何没有循环通过每个项目,因为列表太大了?在组合框中显示一些项目以粗体显示
关闭所选项目。
public Form1()
{
_dataItems = new List<DataItem>
{
new DataItem {Name = "Alpha", IsBold = true, OtherData = new object()},
new DataItem {Name = "Beta", IsBold = false, OtherData = new object()},
new DataItem {Name = "Gamma", IsBold = true, OtherData = new object()},
};
this.InitializeComponent();
comboBox1.DrawItem += comboBox1_DrawItem;
comboBox1.DataSource = _dataItems;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "OtherData";
}
void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
var dataItem = (DataItem)comboBox1.Items[e.Index];
if (dataItem.IsBold)
e.Graphics.DrawString(dataItem.Name, BoldFont, SystemBrushes.ControlText,
e.Bounds);
else
e.Graphics.DrawString(dataItem.Name, NormalFont, SystemBrushes.ControlText,
e.Bounds);
}
的DataItem的类:
public class DataItem
{
public String Name { get; set; }
public bool IsBold { get; set; }
public Object OtherData { get; set; }
public override string ToString()
{
return Name;
}
}
变种的DataItem =(DataItem的)comboBox1.Items [e.Index];将数据绑定到控件之前是否可以访问items集合?数据源在上面的行后面分配。 – bhu 2012-03-29 08:25:20
@bhu我改变了代码来使用传统的功能,而不是LINQ – 2012-03-29 16:25:04