I have an object stored in a database that I am retrieving in my viewmodel and placing into an observable collection. These objects are properties (houses/realestate) that each have a child object called Images. Each property can have multiple images (but each image can only have one property). I only want to use one viewmodel. I have the properties populating a listbox fine, and I can successfully bind the images to a subsequent listbox, but only if I do it via iList. My question is how do I implement the Images into an observable collection of their own (so I can monitor changes), in place of the iList. Here is the code of some of the features I mentioned above…
public IList<Image> Images
{
get
{
if (CurrentProperty != null)
return CurrentProperty.Images.ToList();
return null;
}
}
private void Load()
{
PropertyList = new ObservableCollection<Property>(from property in entities.Properties.Include("Images") select property);
//Sort the list (based on previous session stored in database)
var sortList = PropertyList.OrderBy(x => x.Sort).ToList();
PropertyList.Clear();
sortList.ForEach(PropertyList.Add);
propertyView = CollectionViewSource.GetDefaultView(PropertyList);
if (propertyView != null) propertyView.CurrentChanged += new System.EventHandler(propertyView_CurrentChanged);
public const string PropertiesPropertyName = "PropertyList";
private ObservableCollection<Property> _PropertyList = null;
public ObservableCollection<Property> PropertyList
{
get
{
return _PropertyList;
}
set
{
if (_PropertyList == value)
{
return;
}
var oldValue = _PropertyList;
_PropertyList = value;
// Update bindings, no broadcast
RaisePropertyChanged(PropertiesPropertyName);
}
}
Ended up solving the issue by following the answer provided on this question:
Listview to display ObservableSelection
I created an observablecollection for the images and then created a new method images_Update() , which is called everytime the current item of the view (of the properties observable collection) changes. I also slotted it in at the bottom of the AddImage() and DeleteImage() method to ensure that when they are called the list is updated.