Skip to content

Fix ChildChanged event not being triggered for BusinessListBase objects in Blazor ViewModel#4747

Merged
rockfordlhotka merged 2 commits into
mainfrom
copilot/fix-childchanged-event-trigger
Oct 20, 2025
Merged

Fix ChildChanged event not being triggered for BusinessListBase objects in Blazor ViewModel#4747
rockfordlhotka merged 2 commits into
mainfrom
copilot/fix-childchanged-event-trigger

Conversation

Copilot AI commented Oct 20, 2025

Copy link
Copy Markdown
Contributor

Problem

The ChildChanged event on the Blazor ViewModel was not being triggered when the Model object is of type BusinessListBase. The event hooks only worked for IBusinessBase objects, excluding collection types like BusinessListBase.

Root Cause

The HookChangedEvents and UnhookChangedEvents methods in Csla.Blazor.ViewModel<T> were checking for the IBusinessBase interface:

if (model is IBusinessBase ncc)
    ncc.ChildChanged += OnModelChildChanged;

While BusinessBase implements IBusinessBase (which extends INotifyChildChanged), BusinessListBase does not implement IBusinessBase. Instead, it inherits from ObservableBindingList<T> which directly implements INotifyChildChanged.

Solution

Changed both methods to check for INotifyChildChanged instead of IBusinessBase:

if (model is INotifyChildChanged ncc)
    ncc.ChildChanged += OnModelChildChanged;

This works because:

  • IBusinessBase extends INotifyChildChangedBusinessBase objects continue to work
  • ObservableBindingList<T> implements INotifyChildChangedBusinessListBase objects now work

Changes

  • Modified: Source/Csla.Blazor/ViewModel.cs (2 lines)

    • Updated HookChangedEvents to check for INotifyChildChanged
    • Updated UnhookChangedEvents to check for INotifyChildChanged
  • Added: Source/tests/Csla.Blazor.Test/ViewModelChildChangedEventTests.cs

    • Test for BusinessBase objects hooking ChildChanged event
    • Test for BusinessListBase objects hooking ChildChanged event
    • Test for proper unhooking when model changes

Benefits

Backwards Compatible: All existing BusinessBase objects continue to work
Fixes the Issue: BusinessListBase objects now properly hook ChildChanged events
Aligned with Existing Code: Matches the pattern in Csla.Xaml.Shared/ViewModelBase.cs
Minimal Change: Only 2 lines of production code modified
Well Tested: Comprehensive unit tests added to prevent regression

Fixes #[issue_number]

Original prompt

This section details on the original issue you should resolve

<issue_title>ChildChanged Event not being triggered in the Blazor ViewModel for BusinessListBase objects</issue_title>
<issue_description>Describe the bug
The ChildChanged event on the Blazor ViewModel is not getting triggered when the Model object is of type BusinessListBase. It looks like it only triggers for IBusinessBase objects.

Version and Platform
CSLA version: 9.1.0
OS: Windows
Platform: Blazor Server

Code that Fails

/// <summary>
    /// Unhooks changed event handlers from the model.
    /// </summary>
    /// <param name="model"></param>
    protected void UnhookChangedEvents(T? model)
    {
      if (model is INotifyPropertyChanged npc)
        npc.PropertyChanged -= OnModelPropertyChanged;

      if (model is IBusinessBase ncc)
        ncc.ChildChanged -= OnModelChildChanged;

      if (model is INotifyCollectionChanged cc)
        cc.CollectionChanged -= OnModelCollectionChanged;
    }

    /// <summary>
    /// Hooks changed events on the model.
    /// </summary>
    /// <param name="model"></param>
    private void HookChangedEvents(T? model)
    {
      if (model is INotifyPropertyChanged npc)
        npc.PropertyChanged += OnModelPropertyChanged;

      if (model is IBusinessBase ncc)
        ncc.ChildChanged += OnModelChildChanged;

      if (model is INotifyCollectionChanged cc)
        cc.CollectionChanged += OnModelCollectionChanged;
    }

Additional context
I think changing the above code to check for INotifyChildChanged instead of IBusinessBase would solve this issue.
</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #4723


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix ChildChanged event not triggered in Blazor ViewModel Fix ChildChanged event not being triggered for BusinessListBase objects in Blazor ViewModel Oct 20, 2025
Copilot AI requested a review from rockfordlhotka October 20, 2025 18:27
@rockfordlhotka rockfordlhotka marked this pull request as ready for review October 20, 2025 18:43
@rockfordlhotka rockfordlhotka merged commit 581560d into main Oct 20, 2025
4 checks passed
@rockfordlhotka rockfordlhotka deleted the copilot/fix-childchanged-event-trigger branch October 20, 2025 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ChildChanged Event not being triggered in the Blazor ViewModel for BusinessListBase objects

2 participants