SHOULD I USE AN OBSERVABLECOLLECTION FOR IMAGES IN WPF?

C G E Mannerheim

Total Post:29

Points:203
Posted by  C G E Mannerheim
WPF 
WPF
 1650  View(s)
Ratings:
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");

    }

  1. Pravesh Singh

    Post:412

    Points:2888
    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:

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!