1

I am trying to register the PropertyChanged event for an Observable Collection in PowerShell, but no dice. This works just fine for the CollectionChanged event.

This works:

Register-ObjectEvent -InputObject $MyObservableCollection -EventName CollectionChanged -Action {Write-Host "Collection changed!"}`

Whereas this does not work:

Register-ObjectEvent -InputObject $MyObservableCollection -EventName PropertyChanged -Action {Write-Host "Property changed!"}`

The error I receive is as follows:

Register-ObjectEvent : Cannot register for the specified event. An event with the name 'PropertyChanged' does not exist.

3
  • 1
    Please can you post how you are creating the $MyObservableCollection, and also the error your getting. Commented Aug 19, 2023 at 9:09
  • Sure: $MyObservableCollection = New-Object -TypeName System.Collections.ObjectModel.ObservableCollection[Object] Register-ObjectEvent -InputObject $MyObservableCollection -EventName PropertyChanged -Action {Write-Host "Hello"} Register-ObjectEvent : Cannot register for the specified event. An event with the name 'PropertyChanged' does not exist. Commented Aug 19, 2023 at 9:13
  • 1
    The PropertyChanged event on an ObservableCollection<T> is protected which means it can only be accessed by that class and any classes that inherit from it - see learn.microsoft.com/en-us/dotnet/api/… Commented Aug 19, 2023 at 14:19

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, KG-DROID. Just out of interest, is PowerShell not capable of creating the required class as you have in C#?
As far as I'm aware, PowerShell classes don't do events like this, even if they did though, PowerShell is based on C# anyway, so in the backend code it would most likely work in the same way anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.