C# 将图像添加到列表框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/676869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 13:03:23  来源:igfitidea点击:

Add image to listbox

c#wpfimagelistboxreturn-value

提问by Anand Shah

I have a few images with some text, I need to show the image with the relevant text in a listbox.

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

Browsing google I came across this sample class,

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

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"));

The above code works fine as it only returns string, how do I return an image and a string together so that it gets added to the listbox?

上面的代码工作正常,因为它只返回字符串,如何将图像和字符串一起返回,以便将其添加到列表框?

Best Regards

此致

@nand

@南德

采纳答案by Josh G

Just use a DataTemplateto display your objects in the ListBox.

只需使用 aDataTemplateListBox.

Create a data object that contains string properties and an Image property:

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

public class Img
{
    public Img(string value, Image img) { Str = value; Image = img; }
    public string Str { get; set; }
    public Image Image { get; set; }
}

Create a DataTemplateto display this:

创建一个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>

Now add the Imgitems (or your data objects) to the ListBoxlike so:

现在将Img项目(或您的数据对象)添加到ListBox这样的:

lstBox.Items.Add(new Img("Value", myImage));

回答by Program.X

You can't (without hacking around) put images in ListBoxes.

您不能(不进行修改)将图像放入 ListBox 中。

You can put them in ListViews.

您可以将它们放在 ListViews 中。

You need to put your image(s) in an ImageList component, then attach the ImageList to your listview. Of course, you can encapsulate your image in your class by adding an Image property and adding that to the ImageList.Items collection.

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

Then for each ListViewItem in the List, set the ImageIndex property to the index of the image in the listview.

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

All this can be done using the designer.

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

回答by Anirudh Goel

You can add bitmapsource objects to a listboxitem and add it to listbox. Check this thread. http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f6b7f985-f11b-4d7f-845a-44851360ee1f/

您可以将位图源对象添加到列表框项并将其添加到列表框。检查这个线程。 http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f6b7f985-f11b-4d7f-845a-44851360ee1f/

回答by Karthik_Jram

In reply to the question from Abbassi - but I got an error "local" is an undeclared namespace

回答 Abbassi 的问题 -但我收到一个错误“local”是一个未声明的命名空间

Refer the following:

请参考以下内容:

Add "local" namespace in Window tag.

在 Window 标签中添加“本地”命名空间。

<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">

回答by Lidza

first put in ValueMemeber Image property (in here is String property too) and DrawMode to OwnerDrawVariable and redefine DrawItem

首先放入 ValueMemeber Image 属性(这里也是 String 属性)和 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);
         }
        }