|
-
Nov 5th, 2009, 02:25 PM
#1
Thread Starter
Member
Binding custom objects collection to ListBox in WPF??
Hi
Thanks for putting this example. But, everywhere I find example of ArrayList containg strings. Is there a way to bind a collection of custom objects?Please advise.
Something like this instead of string collection :
public AlbumCollection GetAlbums()
{
var albumCollection = new AlbumCollection();
albumCollection.Album.Add(new Album() { Description = "A1 Desc", Name = "A1 Name", Id = 1 });
albumCollection.Album.Add(new Album() { Description = "A2 Desc", Name = "A2 Name", Id = 2 });
albumCollection.Album.Add(new Album() { Description = "A3 Desc", Name = "A3 Name", Id = 3 });
albumCollection.Album.Add(new Album() { Description = "A4 Desc", Name = "A4 Name", Id = 4 });
return albumCollection;
}
--------------
public partial class AlbumCollection
{
[EditorBrowsable(EditorBrowsableState.Never)]
private List<Album> albumField;
[System.Xml.Serialization.XmlElementAttribute("Album")]
public List<Album> Album
{
get
{
if ((this.albumField == null))
{
this.albumField = new List<Album>();
}
return this.albumField;
}
set
{
this.albumField = value;
}
}
}
public partial class Album
{
[EditorBrowsable(EditorBrowsableState.Never)]
private List<Document> documentField;
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
[System.Xml.Serialization.XmlElementAttribute("Document")]
public List<Document> Document
{
get
{
if ((this.documentField == null))
{
this.documentField = new List<Document>();
}
return this.documentField;
}
set
{
this.documentField = value;
}
}
}
Please advise. Thanks
Pankaj
-
Nov 6th, 2009, 09:16 AM
#2
Re: Binding custom objects collection to ListBox in WPF??
Yeah the examples might just be strings but you bind the array/list to the listbox in the exact same way no matter what is in your list. You just set the ItemsSource property of the listbox to your list - then use a DataTemplate to show the items in the listbox in whatever way you want. Can you post the code/XAML that you are currently using to actually do the binding?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|