|
1 | | -namespace Caliburn.Micro { |
| 1 | +namespace Caliburn.Micro |
| 2 | +{ |
2 | 3 | using System.Linq; |
3 | 4 |
|
4 | 5 |
|
|
15 | 16 | /// </summary> |
16 | 17 | /// <typeparam name="T">The type of item in the attached collection.</typeparam> |
17 | 18 | public class AttachedCollection<T> : DependencyObjectCollection, IAttachedObject |
18 | | - where T : DependencyObject, IAttachedObject { |
| 19 | + where T : DependencyObject, IAttachedObject |
| 20 | + { |
19 | 21 | private DependencyObject associatedObject; |
20 | 22 |
|
21 | 23 | /// <summary> |
22 | 24 | /// Creates an instance of <see cref="AttachedCollection<T>"/> |
23 | 25 | /// </summary> |
24 | | - public AttachedCollection() { |
| 26 | + public AttachedCollection() |
| 27 | + { |
25 | 28 | VectorChanged += OnVectorChanged; |
26 | 29 | } |
27 | 30 |
|
28 | 31 | /// <summary> |
29 | 32 | /// Attaches the collection. |
30 | 33 | /// </summary> |
31 | 34 | /// <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 | + { |
33 | 37 | associatedObject = dependencyObject; |
34 | 38 | this.OfType<IAttachedObject>().Apply(x => x.Attach(associatedObject)); |
35 | 39 | } |
36 | 40 |
|
37 | 41 | /// <summary> |
38 | 42 | /// Detaches the collection. |
39 | 43 | /// </summary> |
40 | | - public void Detach() { |
| 44 | + public void Detach() |
| 45 | + { |
41 | 46 | this.OfType<IAttachedObject>().Apply(x => x.Detach()); |
42 | 47 | associatedObject = null; |
43 | 48 | } |
44 | 49 |
|
45 | 50 | /// <summary> |
46 | 51 | /// The currently attached object. |
47 | 52 | /// </summary> |
48 | | - public DependencyObject AssociatedObject { |
| 53 | + public DependencyObject AssociatedObject |
| 54 | + { |
49 | 55 | get { return associatedObject; } |
50 | 56 | } |
51 | 57 |
|
52 | 58 | /// <summary> |
53 | 59 | /// Called when an item is added from the collection. |
54 | 60 | /// </summary> |
55 | 61 | /// <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); |
60 | 67 | } |
61 | 68 | } |
62 | 69 |
|
63 | 70 | /// <summary> |
64 | 71 | /// Called when an item is removed from the collection. |
65 | 72 | /// </summary> |
66 | 73 | /// <param name="item">The item that was removed.</param> |
67 | | - protected virtual void OnItemRemoved(DependencyObject item) { |
| 74 | + protected virtual void OnItemRemoved(DependencyObject item) |
| 75 | + { |
68 | 76 | var attached = item as IAttachedObject; |
69 | | - if (attached != null && attached.AssociatedObject != null) { |
| 77 | + if (attached != null && attached.AssociatedObject != null) |
| 78 | + { |
70 | 79 | attached.Detach(); |
71 | 80 | } |
72 | 81 | } |
73 | 82 |
|
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 | + { |
76 | 87 | case CollectionChange.ItemInserted: |
77 | | - OnItemAdded(this[(int) @event.Index]); |
| 88 | + OnItemAdded(this[(int)@event.Index]); |
78 | 89 | break; |
79 | 90 | case CollectionChange.ItemRemoved: |
80 | | - OnItemRemoved(this[(int) @event.Index]); |
| 91 | + OnItemRemoved(this[(int)@event.Index]); |
81 | 92 | break; |
82 | 93 | case CollectionChange.Reset: |
83 | 94 | this.Apply(OnItemRemoved); |
|
0 commit comments