Add image to list

I have several images with some text, I need to show an image with the corresponding text in the list.

Browse 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 because it only returns a string, how do I return an image and a string together so that it is added to the list?

Best wishes

@nand

+4
source share
5 answers

Just use a DataTemplate to display your objects in a ListBox .

Create a data object that contains the row properties and the 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 DataTemplate to display this:

 <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 Img elements (or your data objects) to the ListBox like this:

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

You cannot (without hacking) put images in ListBoxes.

You can put them in ListViews.

You need to put your images in the ImageList component and then bind ImageList to the list. Of course, you can encapsulate an image in your class by adding the Image property and adding it to the ImageList.Items collection.

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

All this can be done using the constructor.

+3
source

first insert the ValueMemeber Image property (here also the String property) and DrawMode to OwnerDrawVariable and override 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); } } 
+1
source

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

0
source

In response to a question from Abbassi - but I got the error message "local" - this is an undeclared namespace

Please note the following:

Add a "local" namespace in the Windows tag.

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

All Articles