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
Just use a
DataTemplate
to display your objects in theListBox
.Create a data object that contains string properties and an Image property:
Create a
DataTemplate
to display this:Now add the
Img
items (or your data objects) to theListBox
like so:first put in ValueMemeber Image property (in here is String property too) and DrawMode to OwnerDrawVariable and redefine DrawItem
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/
You can't (without hacking around) put images in ListBoxes.
You can put them in 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.
Then for each ListViewItem in the List, set the ImageIndex property to the index of the image in the listview.
All this can be done using the designer.
In reply to the question from Abbassi - but I got an error "local" is an undeclared namespace
Refer the following:
Add "local" namespace in Window tag.