-
Notifications
You must be signed in to change notification settings - Fork 777
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unset Parent on Conductors' Items.Clear()
ObservableCollection<T> is designed so NotifyCollectionChangedEventArgs.OldItems is not populated when .Clear() is called on the collection. Because of this, Caliburn conductors can't unset the Parent property on all conducted IChild. There are multiple solutions to this issue that have been discussed, but I feel like adding a new CollectionCleared event to BindableCollection is the best compromise considering it doesn't break any existing code and only extends existing ObservableCollection functionality.
- Loading branch information
1 parent
8eacda6
commit d3e33ed
Showing
7 changed files
with
76 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Xunit; | ||
|
||
namespace Caliburn.Micro.Core.Tests | ||
{ | ||
public class BindableCollectionTests | ||
{ | ||
[Fact] | ||
public void Clear_ThenCollectionClearedIsRaisedWithAllItems() | ||
{ | ||
var objects = new[] { new object(), new object(), new object() }; | ||
var bindableCollection = new BindableCollection<object>(objects); | ||
|
||
var collectionClearedEvent = Assert.Raises<CollectionClearedEventArgs<object>>( | ||
h => bindableCollection.CollectionCleared += h, h => bindableCollection.CollectionCleared -= h, | ||
() => bindableCollection.Clear() | ||
); | ||
|
||
Assert.Equal(objects, collectionClearedEvent.Arguments.ClearedItems); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Caliburn.Micro.Core.Tests/ConductorWithCollectionAllActiveTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Xunit; | ||
|
||
namespace Caliburn.Micro.Core.Tests | ||
{ | ||
public class ConductorCollectionAllActiveTests | ||
{ | ||
[Fact] | ||
public void ParentItemIsUnsetOnClear() | ||
{ | ||
var conductor = new Conductor<IScreen>.Collection.AllActive(); | ||
var conducted = new[] { new Screen(), new Screen() }; | ||
conductor.Items.AddRange(conducted); | ||
|
||
conductor.Items.Clear(); | ||
|
||
Assert.All(conducted, screen => Assert.NotEqual(conductor, screen.Parent)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Caliburn.Micro | ||
{ | ||
public class CollectionClearedEventArgs<T> : EventArgs | ||
{ | ||
public CollectionClearedEventArgs(IReadOnlyCollection<T> clearedItems) | ||
{ | ||
ClearedItems = clearedItems; | ||
} | ||
|
||
public IReadOnlyCollection<T> ClearedItems { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters