Skip to content

Commit d308202

Browse files
authored
Merge pull request #987 from Caliburn-Micro/alert-fix-72
missed if statement
2 parents e555b88 + 2f9ab2a commit d308202

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

src/Caliburn.Micro.Platform/Platforms/uap/AttachedCollection.81.cs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
namespace Caliburn.Micro {
1+
namespace Caliburn.Micro
2+
{
23
using System.Linq;
34

45

@@ -15,69 +16,79 @@
1516
/// </summary>
1617
/// <typeparam name="T">The type of item in the attached collection.</typeparam>
1718
public class AttachedCollection<T> : DependencyObjectCollection, IAttachedObject
18-
where T : DependencyObject, IAttachedObject {
19+
where T : DependencyObject, IAttachedObject
20+
{
1921
private DependencyObject associatedObject;
2022

2123
/// <summary>
2224
/// Creates an instance of <see cref="AttachedCollection&lt;T&gt;"/>
2325
/// </summary>
24-
public AttachedCollection() {
26+
public AttachedCollection()
27+
{
2528
VectorChanged += OnVectorChanged;
2629
}
2730

2831
/// <summary>
2932
/// Attaches the collection.
3033
/// </summary>
3134
/// <param name="dependencyObject">The dependency object to attach the collection to.</param>
32-
public void Attach(DependencyObject dependencyObject) {
35+
public void Attach(DependencyObject dependencyObject)
36+
{
3337
associatedObject = dependencyObject;
3438
this.OfType<IAttachedObject>().Apply(x => x.Attach(associatedObject));
3539
}
3640

3741
/// <summary>
3842
/// Detaches the collection.
3943
/// </summary>
40-
public void Detach() {
44+
public void Detach()
45+
{
4146
this.OfType<IAttachedObject>().Apply(x => x.Detach());
4247
associatedObject = null;
4348
}
4449

4550
/// <summary>
4651
/// The currently attached object.
4752
/// </summary>
48-
public DependencyObject AssociatedObject {
53+
public DependencyObject AssociatedObject
54+
{
4955
get { return associatedObject; }
5056
}
5157

5258
/// <summary>
5359
/// Called when an item is added from the collection.
5460
/// </summary>
5561
/// <param name="item">The item that was added.</param>
56-
protected virtual void OnItemAdded(DependencyObject item) {
57-
if (associatedObject != null) {
58-
if (item is IAttachedObject)
59-
((IAttachedObject) item).Attach(associatedObject);
62+
protected virtual void OnItemAdded(DependencyObject item)
63+
{
64+
if (associatedObject != null && item is IAttachedObject attached)
65+
{
66+
attached.Attach(associatedObject);
6067
}
6168
}
6269

6370
/// <summary>
6471
/// Called when an item is removed from the collection.
6572
/// </summary>
6673
/// <param name="item">The item that was removed.</param>
67-
protected virtual void OnItemRemoved(DependencyObject item) {
74+
protected virtual void OnItemRemoved(DependencyObject item)
75+
{
6876
var attached = item as IAttachedObject;
69-
if (attached != null && attached.AssociatedObject != null) {
77+
if (attached != null && attached.AssociatedObject != null)
78+
{
7079
attached.Detach();
7180
}
7281
}
7382

74-
private void OnVectorChanged(IObservableVector<DependencyObject> sender, IVectorChangedEventArgs @event) {
75-
switch (@event.CollectionChange) {
83+
private void OnVectorChanged(IObservableVector<DependencyObject> sender, IVectorChangedEventArgs @event)
84+
{
85+
switch (@event.CollectionChange)
86+
{
7687
case CollectionChange.ItemInserted:
77-
OnItemAdded(this[(int) @event.Index]);
88+
OnItemAdded(this[(int)@event.Index]);
7889
break;
7990
case CollectionChange.ItemRemoved:
80-
OnItemRemoved(this[(int) @event.Index]);
91+
OnItemRemoved(this[(int)@event.Index]);
8192
break;
8293
case CollectionChange.Reset:
8394
this.Apply(OnItemRemoved);

0 commit comments

Comments
 (0)