多列数据绑定组合框? [英] multi column data bound combobox ?

查看:77
本文介绍了多列数据绑定组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常感谢.你能给我解释一下这些程序吗

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. See

protected override void OnDrawItem(DrawItemEventArgs e)

It uses Graphics object to draw headers (Columnnames ) first and then Draws a string for each items.

The other one
protected 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:


这篇关于多列数据绑定组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆