C# 将图像添加到列表框

C# 将图像添加到列表框,c#,wpf,image,listbox,return-value,C#,Wpf,Image,Listbox,Return Value,我有一些带有文本的图像,我需要在列表框中显示带有相关文本的图像 浏览谷歌时,我遇到了这个示例类 public class Customer { public string Fname; public string Lname; public Customer(string firstName, string lastName) { Fname = firstName; Lname = lastName; } public override string T

我有一些带有文本的图像,我需要在列表框中显示带有相关文本的图像

浏览谷歌时,我遇到了这个示例类

public class Customer
{

    public string Fname;

    public string Lname;

public Customer(string firstName, string lastName)
{
    Fname = firstName;
    Lname = lastName;
}

public override string ToString()
{
    return Fname + " " + Lname;
}
}

lstCustomers.Items.Add(new Customer("Foo","Bar"));
上面的代码运行良好,因为它只返回字符串,如何将图像和字符串一起返回,以便将其添加到列表框中

致意

@nand

您不能(不经过黑客攻击)将图像放入列表框

您可以将它们放在列表视图中

您需要将图像放入ImageList组件中,然后将ImageList附加到listview。当然,您可以通过添加图像属性并将其添加到ImageList.Items集合中,在类中封装图像

然后,对于列表中的每个ListViewItem,将ImageIndex属性设置为listview中图像的索引


所有这些都可以使用设计器完成。

您可以将bitmapsource对象添加到listboxitem并将其添加到listbox。 检查这条线。

只需使用
数据模板
即可在
列表框中显示对象

创建包含字符串属性和图像属性的数据对象:

public class Img
{
    public Img(string value, Image img) { Str = value; Image = img; }
    public string Str { get; set; }
    public Image Image { get; set; }
}
创建一个
DataTemplate
来显示:

<ListBox x:Name="lstBox">
    <ListBox.ItemTemplate>
        <DataTemplate DataType="{x:Type local:Img}">
            <StackPanel>
                <TextBlock Margin="3" Text="{Binding Str}"/>
                <ContentControl Margin="3" Content="{Binding Image}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

在回答Abbassi的问题时,我得到了一个错误,“local”是一个未声明的名称空间

请参阅以下内容:

在窗口标记中添加“本地”命名空间

<Window x:Class="MyApp.MyWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MyWindow" Height="400" Width="600"
    xmlns:local="clr-namespace:MyApp">

首先将ValueMemeber图像属性(这里也是字符串属性)和DrawMode放入OwnerDrawVariable并重新定义DrawItem

listbox1.DrawItem += new DrawItemEventHandler(listbox1_DrawItem);
listbox1.ItemHeight = 16;

private void listbox1_DrawItem(object sender, DrawItemEventArgs e)
       { 
e.DrawBackground();
        e.DrawFocusRectangle();
        Rectangle bounds = e.Bounds;
        Size imageSize = new Size(16, 16);
        Bitmap b;
        StringFormat sf = new StringFormat();
        sf.Alignment = StringAlignment.Far;
        Rectangle rc = new Rectangle(e.Bounds.X + 1, e.Bounds.Y + 1, e.Bounds.Width - 5, e.Bounds.Height - 3);
         UseObject s ;
         if (e.Index >= 0)
         {
             s = (UseObject)listbox1.Items[e.Index];

             b = new Bitmap(s.Img, imageSize);
             e.Graphics.DrawImage(b, e.Bounds.X, e.Bounds.Y);
             e.Graphics.DrawString(s.Str, new Font("Verdana", 10, FontStyle.Bold), new SolidBrush(Color.Black), rc, sf);
         }
        }

看起来你指的是WPF?或者WinForms?我建议添加适当的标记以澄清。这是否也适用于WinForms,但不确定datatemplate以及如何在表单上创建它?谢谢。不,这不适用于Windows窗体。窗口窗体没有数据模板或XAML。对于Windows窗体,您可能需要编写自己的代码来生成数据项的控件。我收到“错误1名称“Img”不存在于关于此行的命名空间“TheseusAndTheMinotaur”中
。但我已将Img类放入命名空间TheseusAndTheMinotaur中。
listbox1.DrawItem += new DrawItemEventHandler(listbox1_DrawItem);
listbox1.ItemHeight = 16;

private void listbox1_DrawItem(object sender, DrawItemEventArgs e)
       { 
e.DrawBackground();
        e.DrawFocusRectangle();
        Rectangle bounds = e.Bounds;
        Size imageSize = new Size(16, 16);
        Bitmap b;
        StringFormat sf = new StringFormat();
        sf.Alignment = StringAlignment.Far;
        Rectangle rc = new Rectangle(e.Bounds.X + 1, e.Bounds.Y + 1, e.Bounds.Width - 5, e.Bounds.Height - 3);
         UseObject s ;
         if (e.Index >= 0)
         {
             s = (UseObject)listbox1.Items[e.Index];

             b = new Bitmap(s.Img, imageSize);
             e.Graphics.DrawImage(b, e.Bounds.X, e.Bounds.Y);
             e.Graphics.DrawString(s.Str, new Font("Verdana", 10, FontStyle.Bold), new SolidBrush(Color.Black), rc, sf);
         }
        }