[英]Placing Images and Strings with a C# Combobox
我需要為 Windows 窗體應用程序創建一個下拉菜單或組合框,其中包含一個小圖像,然后是旁邊的一串文本。 基本上,您可以將下拉列表中的每一“行”視為需要有一個圖標,然后是圖標右側的圖標名稱。 我在做這件事時遇到了麻煩——事實上,我完全沒有成功。 有誰知道完成這項任務的方法? 任何幫助將不勝感激。 謝謝!
我能夠想出一些非常簡單的代碼來執行此操作(請參閱下面的代碼段)。 該代碼創建了一個控件,它是一個下拉控件,它在同一行中顯示一個小的彩色方塊和該顏色的名稱(見圖)。 感謝您在最初發布時為此提供的鏈接! 希望此控件可以在將來幫助其他人。
圖片:
代碼:
class ColorSelector : ComboBox
{
public ColorSelector()
{
DrawMode = DrawMode.OwnerDrawFixed;
DropDownStyle = ComboBoxStyle.DropDownList;
}
// Draws the items into the ColorSelector object
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
DropDownItem item = new DropDownItem(Items[e.Index].ToString());
// Draw the colored 16 x 16 square
e.Graphics.DrawImage(item.Image, e.Bounds.Left, e.Bounds.Top);
// Draw the value (in this case, the color name)
e.Graphics.DrawString(item.Value, e.Font, new
SolidBrush(e.ForeColor), e.Bounds.Left + item.Image.Width, e.Bounds.Top + 2);
base.OnDrawItem(e);
}
}
public class DropDownItem
{
public string Value
{
get { return value; }
set { this.value = value; }
}
private string value;
public Image Image
{
get { return img; }
set { img = value; }
}
private Image img;
public DropDownItem() : this("")
{}
public DropDownItem(string val)
{
value = val;
this.img = new Bitmap(16, 16);
Graphics g = Graphics.FromImage(img);
Brush b = new SolidBrush(Color.FromName(val));
g.DrawRectangle(Pens.White, 0, 0, img.Width, img.Height);
g.FillRectangle(b, 1, 1, img.Width - 1, img.Height - 1);
}
public override string ToString()
{
return value;
}
}
非常有幫助..一些優化:
public sealed class ColorSelector : ComboBox
{
public ColorSelector()
{
DrawMode = DrawMode.OwnerDrawFixed;
DropDownStyle = ComboBoxStyle.DropDownList;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
if (e.Index >= 0 && e.Index < Items.Count)
{
DropDownItem item = (DropDownItem)Items[e.Index];
e.Graphics.DrawImage(item.Image, e.Bounds.Left, e.Bounds.Top);
e.Graphics.DrawString(item.Value, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + item.Image.Width, e.Bounds.Top + 2);
}
base.OnDrawItem(e);
}
}
和...
public sealed class DropDownItem
{
public string Value { get; set; }
public Image Image { get; set; }
public DropDownItem()
: this("")
{ }
public DropDownItem(string val)
{
Value = val;
Image = new Bitmap(16, 16);
using (Graphics g = Graphics.FromImage(Image))
{
using (Brush b = new SolidBrush(Color.FromName(val)))
{
g.DrawRectangle(Pens.White, 0, 0, Image.Width, Image.Height);
g.FillRectangle(b, 1, 1, Image.Width - 1, Image.Height - 1);
}
}
}
public override string ToString()
{
return Value;
}
}
注意:此代碼來自用戶 que dal 的優化如果您希望有一個不只是顏色名稱的字符串,請將 DropDownItem 更改為具有 2 個參數,即字符串和顏色,然后只需更改畫筆設置顏色的方式,因此:
public DropDownItem(string val, Color color)
{
Value = val;
Image = new Bitmap(16, 16);
using (Graphics g = Graphics.FromImage(Image))
{
using (Brush b = new SolidBrush(color))
{
g.DrawRectangle(Pens.White, 0, 0, Image.Width, Image.Height);
g.FillRectangle(b, 1, 1, Image.Width - 1, Image.Height - 1);
}
}
}
然后,您必須像這樣更改下拉項:
public DropDownItem()
: this("", Color.Empty)
{}
希望這有幫助:)
我解決了這個問題,我這樣做了:
ComboBox MarcadorNS = new ComboBox(); MarcadorNS.Height = 30; MarcadorNS.Width = 150; MarcadorNS.SelectedValuePath = "Uid"; foreach (var temporalItem in GetPredefinedKinds()) { Image ImagenCombo = new Image(); ImagenCombo.Source = new BitmapImage(new Uri( "Imagenes/Marcadores/" + temporalItem.Name.ToLower() + ".png", UriKind.Absolute)); ImagenCombo.Height = 28; ImagenCombo.Width = 28; ImagenCombo.VerticalAlignment = VerticalAlignment.Top; ImagenCombo.HorizontalAlignment = HorizontalAlignment.Left; Label textoCombo = new Label(); textoCombo.VerticalAlignment = VerticalAlignment.Top; textoCombo.HorizontalAlignment = HorizontalAlignment.Left; textoCombo.Content = BaseDatos.NombresDeMarcadores(temporalItem.ToString()); Grid GridCombo = new Grid(); GridCombo.Uid = ObtenerMarcador(temporalItem.ToString()); StackPanel stackCombo = new StackPanel(); stackCombo.Orientation = Orientation.Horizontal; stackCombo.Children.Add(ImagenCombo); stackCombo.Children.Add(textoCombo); GridCombo.Children.Add(stackCombo); MarcadorNS.Items.Add(GridCombo);
不確定圖像,但這應該適用於字符串:
comboBox.Items.Add("String");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.