您需要做的是,首先將DrawMode
從Normal
更改爲OwnerDrawVariable
。然後你必須處理2個事件:DrawItem
和MeasureItem
。他們會是這樣的:
private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = 40; //Change this to your desired item height
}
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
ComboBox box = sender as ComboBox;
if (Object.ReferenceEquals(null, box))
return;
e.DrawBackground();
if (e.Index >= 0)
{
Graphics g = e.Graphics;
using (Brush brush = ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
? new SolidBrush(SystemColors.Highlight)
: new SolidBrush(e.BackColor))
{
using (Brush textBrush = new SolidBrush(e.ForeColor))
{
g.FillRectangle(brush, e.Bounds);
g.DrawString(box.Items[e.Index].ToString(),
e.Font,
textBrush,
e.Bounds,
StringFormat.GenericDefault);
}
}
}
e.DrawFocusRectangle();
}
請分享控件的設計代碼和什麼需要 – Sajal
我主要是開發Windows Phone和那裏,如果我需要實現這樣我用了一個快速搜索功能的UI圖像用戶鍵入的TextBox,以及在其下呈現搜索結果的列表視圖。它工作得非常好,併爲定製提供了很多空間。 – WPMed
您是否嘗試過更改DrawMode? – Pikoh