Looks like you need to create a class that support the Property changed, so you could do something like this:
$PersonClass = @'
using System.ComponentModel;
public class Person : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _Name;
public string Name
{
get { return _Name; }
set
{
_Name = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
private string _Age;
public string Age
{
get { return _Age; }
set
{
_Age = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Age"));
}
}
private string _Country;
public string Country
{
get { return _Country; }
set
{
_Country = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Country"));
}
}
}
'@
Add-Type -TypeDefinition $PersonClass -Language 'CSharp'
$NewPerson = [Person]::New()
$MyObservableCollection = New-Object -TypeName System.Collections.ObjectModel.ObservableCollection[Person]
Register-ObjectEvent -InputObject $NewPerson -EventName PropertyChanged -Action {Write-Host "PropertyChanged on item"}
Register-ObjectEvent -InputObject $MyObservableCollection -EventName CollectionChanged -Action {Write-Host "Item added or removed"}
$MyObservableCollection.Add($NewPerson)
$NewPerson.Name = 'Joe Bloggs'
Here we are creating a Person class which stores the properties of a person like Name, Age and country, each with a PopertyChangedEventHandler. Once you have that its then creating an observable arraylist so you can add/remove individual Persons from the list (which will trigger the CollectionChanged event).
PropertyChangedevent on anObservableCollection<T>isprotectedwhich means it can only be accessed by that class and any classes that inherit from it - see learn.microsoft.com/en-us/dotnet/api/…