Home > DeveloperSection > Forums > Should I use an ObservableCollection for Images in wpf?
C G E Mannerheim
C G E Mannerheim

Total Post:29

Points:203
Posted on    August-10-2013 5:14 AM

 WPF WPF 
Ratings:


 1 Reply(s)
 1238  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);          

 

        RaisePropertyChanged("CurrentContact");

        RaisePropertyChanged("SaleTitle");

        RaisePropertyChanged("Address");

        RaisePropertyChanged("AuctioneerName");

        RaisePropertyChanged("AgentName");

        RaisePropertyChanged("Price");

        RaisePropertyChanged("NextBid");

        RaisePropertyChanged("Status");

    }



Pravesh Singh

Total Post:411

Points:2881
Posted on    August-10-2013 5:29 AM

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:

PropertyImagesViewModel

    - public Property Property { get; set; }

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

    - public int SelectedIndex { get; set; }

PropertyImagesView

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:

RaisePropertyChanged(null);

will do the trick.


Don't want to miss updates? Please click the below button!

Follow MindStick