0

As for question, I have an ObservableCollection of objects, declared in a UserControl and binded to an ItemsControl.ItemsSource.

Its properties are updated during the software run, eventually.

I would like to catch the update event.

I tried using the CollectionChanged event, but it isn't fired.

The collection is declared as belove:

    public ObservableCollection<BatchSensor> BatchSensors
    {
        get { return (ObservableCollection<BatchSensor>)GetValue(BatchSensorsProperty); }
        set { SetValue(BatchSensorsProperty, value); }
    }
    public static readonly DependencyProperty BatchSensorsProperty =
        DependencyProperty.Register(nameof(BatchSensors), typeof(ObservableCollection<BatchSensor>), typeof(BL_BatchContainer), new UIPropertyMetadata(null));

and initialized as follows

    public BL_BatchContainer(string title, ObservableCollection<BatchSensor> batchSensors)
    {
        InitializeComponent();

        Title = title;
        BatchSensors = new ObservableCollection<BatchSensor>();

        BatchSensors.CollectionChanged += OnListChanged;

        BatchSensors = batchSensors;
    }

The class BatchSensor implements INotifyPropertyChanged and fires property change notifications.

public  class BatchSensor: ClassBase
{
    private bool? _error = null;
    public bool? Error { get => _error; set => SetAndNotifyIfChanged(ref _error, value); }
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
Oiproks
  • 764
  • 1
  • 9
  • 34
  • In case you are asking about updates of individual BatchSensor objects, the BatchSensor class must implement INotifyPropertyChanged and fire the PropertyChanged event in the setter of the relevant properties. – Clemens Mar 03 '23 at 16:26
  • Updated the question to provide an answer to your answer :) – Oiproks Mar 03 '23 at 16:33
  • So what is the problem? Are you asking how to subscribe to the PropertyChanged event? Are you looking for something like [this](https://stackoverflow.com/q/8490533/1136211)? – Clemens Mar 03 '23 at 16:39
  • Despite both the objects in the collection and the view are correctly updated, the event CollectionChanged for the collection never fires. That's what I don't understand. – Oiproks Mar 03 '23 at 16:42
  • That event would only be fired when elements are added to or removed from or replaced in the collection. That is not what happens when an element's properties change. – Clemens Mar 03 '23 at 16:45
  • Yes. That's the whole point. So, back to the original question, is there any way to get an update when an object's property inside a collection gets updated? – Oiproks Mar 03 '23 at 16:46
  • Sure, subscribe to its PropertyChanged event, as shown in the answer to that other question. – Clemens Mar 03 '23 at 16:48
  • The CollectionChanged is NEVER fired. Not even when I init the collection in the costructor. Should I try to add each single BatchSensor to the collection in a foreach loop? – Oiproks Mar 03 '23 at 16:54
  • The important point is that you attach a PropertyChanged handler to every item in the collection. If the collection is populated before you attach the CollectionChanged handler you would of course have to iterate over the already present items. – Clemens Mar 03 '23 at 16:56
  • 1
    Another point, you are attaching the CollectionChanged event to a collection that is never used: `BatchSensors.CollectionChanged += OnListChanged;` followed by `BatchSensors = batchSensors;` is wrong. Revert the order of the statements. Do not even assign `new ObservableCollection();`. – Clemens Mar 03 '23 at 17:07

0 Answers0