0
votes

I have a ViewModel with an Observable Collection Property

 public ObservableCollection<GeographicArea> CurrentSensorAreasList
 {
     get
     {
        return currentSensorAreasList;
     }
     set
     {
        if (currentSensorAreasList != value)
        {
            currentSensorAreasList = value;
            OnPropertyChanged(PROPERTY_NAME_CURRENT_SENSOR_AREAS_LIST);
         }
      }
  }

Then in my xaml i have a binding

ItemsSource="{Binding CurrentSensorAreasList}">

This Observable Collection is updated trought a method that can be call in the viewModel constructor or when a collectionchanged handler from another list gets called.

I just clear the list and then add a fewer new items. While debugging i see all my new items updated on the list. But the UI does not get updated. When i regenerate the viewModel and then this update method gets call in the constructor the list gets updated in the UI.

Any ideas?? I don't know if the problem comes when i call the method from a handler.....

UPDATE #1

As requested i'm going the code when I update the list I have tested two ways to do this update

    private void UpdateList1()
        {
            if (globalAreaManagerList != null && OperationEntity != null) 
            {

              CurrentSensorAreasList.Clear();                         
              CurrentSensorAreasList.AddRange(globalAreaManagerList.Where(x => x != (OperationEntity as AreaManager)).SelectMany(areaRenderer => areaRenderer.AreaList));    

//AddRange is an extension method.

            }
        }
private void UpdateList2()
        {
            if (globalAreaManagerList != null && OperationEntity != null) 
            {
                CurrentSensorAreasList = new ObservableCollection<GeographicArea>(globalAreaManagerList.Where(x => x != (OperationEntity as AreaManager)).SelectMany(areaRenderer => areaRenderer.AreaList))


            }
        }

Both cases works when i call it from the constructor. Then I have Other Lists where the Areas changes, and i get notified via CollectionChanged Handlers.

private void globalAreaManagerList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {

            if (e.NewItems != null)
            {
                foreach (AreaManager newItem in e.NewItems)
                {
                    newItem.AreaList.CollectionChanged += AreaList_CollectionChanged;
                }
            }
            if (e.OldItems != null)
            {
                foreach (AreaManager oldItem in e.OldItems)
                {
                    oldItem.AreaList.CollectionChanged -= AreaList_CollectionChanged;
                }
            }
            UpdateList();
        }
        private void AreaList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            UpdateList();
        }

So when i use UpdateList1 seems to work more times but suddenly the Binding is broken and then this update does not show in the UI.

1
That looks all ok. Please show the code where you are updating the collection. - Clemens
I put the code where i update the list. it seems strange because sometimes works and others not... - isra60

1 Answers

0
votes

If you wish change exactly an instance of collection I recomend using DependecyProperty for that case.

Here is:

    public ObservableCollection<GeographicArea> CurrentSensorAreasList
    {
        get { return (ObservableCollection<GeographicArea>)GetValue(CurrentSensorAreasListProperty); }
        set { SetValue(CurrentSensorAreasListProperty, value); }
    }

    public static readonly DependencyProperty CurrentSensorAreasListProperty =
        DependencyProperty.Register("CurrentSensorAreasList", typeof(ObservableCollection<GeographicArea>), typeof(ownerclass));

Where ownerclass - a name of class where you put this property.

But the better way is create only one instance of ObservaleCollection and then just change its items. I mean Add, Remove, and Clear methods.