1- namespace Caliburn . Micro . Maui
1+ namespace Caliburn . Micro . Maui
22{
3- using System ;
43 using System . Collections . Specialized ;
54 using System . Linq ;
65 using global ::Microsoft . Maui . Controls ;
109 /// </summary>
1110 /// <typeparam name="T">The type of item in the attached collection.</typeparam>
1211 public class AttachedCollection < T > : BindableCollection < BindableObject > , IAttachedObject
13- where T : BindableObject , IAttachedObject
12+ where T : BindableObject , IAttachedObject
1413 {
1514 private BindableObject associatedObject ;
1615
@@ -27,45 +26,51 @@ public void Attach(BindableObject dependencyObject)
2726 /// <summary>
2827 /// Detaches the collection.
2928 /// </summary>
30- public void Detach ( ) {
29+ public void Detach ( )
30+ {
3131 this . OfType < IAttachedObject > ( ) . Apply ( x => x . Detach ( ) ) ;
3232 associatedObject = null ;
3333 }
3434
3535 /// <summary>
3636 /// The currently attached object.
3737 /// </summary>
38- public BindableObject AssociatedObject {
38+ public BindableObject AssociatedObject
39+ {
3940 get { return associatedObject ; }
4041 }
4142
4243 /// <summary>
4344 /// Called when an item is added from the collection.
4445 /// </summary>
4546 /// <param name="item">The item that was added.</param>
46- protected virtual void OnItemAdded ( BindableObject item ) {
47- if ( associatedObject != null ) {
48- if ( item is IAttachedObject )
49- ( ( IAttachedObject ) item ) . Attach ( associatedObject ) ;
47+ protected virtual void OnItemAdded ( BindableObject item )
48+ {
49+ if ( associatedObject != null && item is IAttachedObject attached )
50+ {
51+ attached . Attach ( associatedObject ) ;
5052 }
5153 }
5254
5355 /// <summary>
5456 /// Called when an item is removed from the collection.
5557 /// </summary>
5658 /// <param name="item">The item that was removed.</param>
57- protected virtual void OnItemRemoved ( BindableObject item ) {
58- if ( item is IAttachedObject ) {
59- if ( ( ( IAttachedObject ) item ) . AssociatedObject != null )
60- ( ( IAttachedObject ) item ) . Detach ( ) ;
59+ protected virtual void OnItemRemoved ( BindableObject item )
60+ {
61+ if ( item is IAttachedObject attached &&
62+ attached . AssociatedObject != null )
63+ {
64+ attached . Detach ( ) ;
6165 }
6266 }
6367
6468 /// <summary>
6569 /// Raises the <see cref = "E:System.Collections.ObjectModel.ObservableCollection`1.CollectionChanged" /> event with the provided arguments.
6670 /// </summary>
6771 /// <param name = "e">Arguments of the event being raised.</param>
68- protected override void OnCollectionChanged ( NotifyCollectionChangedEventArgs e ) {
72+ protected override void OnCollectionChanged ( NotifyCollectionChangedEventArgs e )
73+ {
6974 base . OnCollectionChanged ( e ) ;
7075
7176 if ( e . OldItems != null )
0 commit comments