У меня есть ComboBox в WindowsForms, и я рисую элементы вручную. Каждый элемент состоит из изображения и текста (Cell.Image и Cell.Title), поэтому высота элемента составляет 34 пикселя.
Моя проблема в том, что когда я раскрываю ComboBox, виден только 1 элемент. MaxDropDownItems = 4, поэтому ComboBox будет рисовать 4 элемента. Я знаю, что я установил DropDownHeight = 34, но я хочу отображать пустой прямоугольник, когда в ComboBox нет элемента, как на следующем рисунке.
ComboBox без элемента - ОК:
ComboBox только с 1 видимым элементом - Плохо:
Мой класс, полученный из ComboBox:
public class ComboBoxCells : ComboBox
{
private List<Cell> _cells;
public List<Cell> Cells
{
get { return this._cells; }
set
{
this._cells = value;
this.BeginUpdate();
this.Items.Clear();
if (value != null)
this.Items.AddRange(value.ToArray());
this.EndUpdate();
}
}
public ComboBoxCells()
{
this.DrawMode = DrawMode.OwnerDrawVariable;
this.DropDownHeight = 34;
this.DropDownWidth = 200;
this.DropDownStyle = ComboBoxStyle.DropDownList;
this.MaxDropDownItems = 4;
this.DrawItem += new DrawItemEventHandler(ComboBoxCells_DrawItem);
this.MeasureItem += new MeasureItemEventHandler(ComboBoxCells_MeasureItem);
}
private void ComboBoxCells_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
// Draw item inside comboBox
if ((e.State & DrawItemState.ComboBoxEdit) != DrawItemState.ComboBoxEdit && e.Index > -1)
{
Cell item = this.Items[e.Index] as Cell;
e.Graphics.FillRectangle(Brushes.Gray, new Rectangle(e.Bounds.Left + 6, e.Bounds.Top + 6, 22, 22));
e.Graphics.DrawImage(item.Image, new Rectangle(e.Bounds.Left + 7, e.Bounds.Top + 7, 20, 20));
e.Graphics.DrawString(item.Title, e.Font,
new SolidBrush(e.ForeColor), e.Bounds.Left + 34, e.Bounds.Top + 10);
}
// Draw visible text
else if (e.Index > -1)
{
Cell item = this.Items[e.Index] as Cell;
e.Graphics.DrawString(item.Title, e.Font,
new SolidBrush(e.ForeColor), e.Bounds.Left, e.Bounds.Top);
}
e.DrawFocusRectangle();
}
private void ComboBoxCells_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = 34;
}
}
Спасибо