Results 1 to 2 of 2

Thread: Binding custom objects collection to ListBox in WPF??

  1. #1

    Thread Starter
    Member
    Join Date
    May 2008
    Posts
    41

    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

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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
  •  



Click Here to Expand Forum to Full Width