多列数据绑定组合框?
问题描述:
非常感谢.你能给我解释一下这些程序吗
Ok thnx very much. Can you explain me those procedures
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
base.OnMeasureItem(e);
if (DesignMode)
return;
for (int colIndex = 0; colIndex < columnNames.Length; colIndex++)
{
string item = Convert.ToString(FilterItemOnProperty(Items[e.Index], columnNames[colIndex]));
SizeF sizeF = e.Graphics.MeasureString(item, Font);
columnWidths[colIndex] = Math.Max(columnWidths[colIndex], sizeF.Width);
}
float totWidth = CalculateTotalWidth();
e.ItemWidth = (int)totWidth;
}
和
and
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
if (DesignMode)
return;
e.DrawBackground();
Rectangle boundsRect = e.Bounds;
int lastRight = 0;
using (Pen linePen = new Pen(SystemColors.GrayText))
{
using (SolidBrush brush = new SolidBrush(ForeColor))
{
if (columnNames.Length == 0)
{
e.Graphics.DrawString(Convert.ToString(Items[e.Index]), Font, brush, boundsRect);
}
else
{
for (int colIndex = 0; colIndex < columnNames.Length; colIndex++)
{
string item = Convert.ToString(FilterItemOnProperty(Items[e.Index], columnNames[colIndex]));
boundsRect.X = lastRight;
boundsRect.Width = (int)columnWidths[colIndex] + columnPadding;
lastRight = boundsRect.Right;
if (colIndex == valueMemberColumnIndex)
{
using (Font boldFont = new Font(Font, FontStyle.Bold))
{
e.Graphics.DrawString(item, boldFont, brush, boundsRect);
}
}
else
{
e.Graphics.DrawString(item, Font, brush, boundsRect);
}
if (colIndex < columnNames.Length - 1)
{
e.Graphics.DrawLine(linePen, boundsRect.Right, boundsRect.Top, boundsRect.Right, boundsRect.Bottom);
}
}
}
}
}
e.DrawFocusRectangle();
}
答
是的,这是什么问题,我认为代码只是在测量了文本的实际宽度之后才绘制了Items.参见protected override void OnDrawItem(DrawItemEventArgs e)
它使用Graphics对象首先绘制标题(Columnnames),然后为每个项目绘制一个字符串.
另一个protected override void OnMeasureItem(MeasureItemEventArgs e)
实际上会计算组合框的Maximum元素的总宽度,因此组合框下拉列表的大小可能与为所有项目留出空间的大小相同.
:cool:
Yes, what is the problem, I think the code just draws the Items after measuring the actual width of the text. Seeprotected override void OnDrawItem(DrawItemEventArgs e)
It uses Graphics object to draw headers (Columnnames ) first and then Draws a string for each items.
The other oneprotected override void OnMeasureItem(MeasureItemEventArgs e)
actually calculates the total width of the Maximum element of the combobox, so that(perhaps) the size of the combobox dropdown is the same size to have room for all the items.
:cool: