Advertise with us

Should I use an ObservableCollection for Images in wpf?

Posted by  C G E Mannerheim
 2439  View(s)
Rate this:

I am using a mvvm pattern for an application that sources its data from a sql ce database using the Entity Framework version 4. The WPF application only has one view (don't need anymore as the app is not that big). I am displaying a collection of properties from the database in a listbox, by creating an observablecollection in my viewmodel and binding this. This works exactly as expected. The issue is that I now have another listbox (in the same view) that needs to be populated with Images for each property. To be clear, each property has a bunch of images, but each image is only assigned to one property.

What would be the best way to display the images, I thought maybe creating another observablecollection for the images, but I am not sure how I would then ensure that only images for the appropriate property are shown. Or should I simply bind the listbox to the Images property of each property (house)?


    private void Load()


        PropertyList = new ObservableCollection<Property>((from property in entities.Properties.Include("Images")

                                                          select property));

        propertyView = CollectionViewSource.GetDefaultView(PropertyList);

        if (propertyView != null)

            propertyView.CurrentChanged += new System.EventHandler(propertyView_CurrentChanged);          











  1. Re: Should I use an ObservableCollection for Images in wpf?

    That sounds distinctly like a different responsibility (a master/details view). In the true spirit of MVVM I'd create a new View and a new ViewModel - perhaps:

        - public Property Property { get; set; }
        - public IList<Image> Images { get; set; }
        - public int SelectedIndex { get; set; }


    Don't forgot to call RaisePropertyChanged() in each of the property setters

    Also note that ObservableCollection does nothing if you aren't manipulating the contents one-at-a-time. If all do you is update the entire collection all-at-once, then it gives you no tangible benefit.

    Another thing - if you need to notify that all your properties changed:


    will do the trick.

      Modified On Mar-30-2018 06:33:01 AM