@ -33,7 +33,7 @@ namespace Avalonia.Controls
public static ItemsSourceView Empty { get ; } = new ItemsSourceView ( Array . Empty < object > ( ) ) ;
public static ItemsSourceView Empty { get ; } = new ItemsSourceView ( Array . Empty < object > ( ) ) ;
private protected readonly IList _ inner ;
private protected readonly IList _ inner ;
private I NotifyCollectionChanged? _ notifyC ollectionChanged;
private NotifyCollectionChangedEventHandler ? _ c ollectionChanged;
/// <summary>
/// <summary>
/// Initializes a new instance of the ItemsSourceView class for the specified data source.
/// Initializes a new instance of the ItemsSourceView class for the specified data source.
@ -55,8 +55,6 @@ namespace Avalonia.Controls
{
{
_ inner = new List < object > ( source . Cast < object > ( ) ) ;
_ inner = new List < object > ( source . Cast < object > ( ) ) ;
}
}
ListenToCollectionChanges ( ) ;
}
}
/// <summary>
/// <summary>
@ -82,14 +80,41 @@ namespace Avalonia.Controls
/// <summary>
/// <summary>
/// Occurs when the collection has changed to indicate the reason for the change and which items changed.
/// Occurs when the collection has changed to indicate the reason for the change and which items changed.
/// </summary>
/// </summary>
public event NotifyCollectionChangedEventHandler ? CollectionChanged ;
public event NotifyCollectionChangedEventHandler ? CollectionChanged
{
add
{
if ( _ collectionChanged is null )
{
if ( _ inner is INotifyCollectionChanged incc )
{
incc . CollectionChanged + = OnCollectionChanged ;
}
}
_ collectionChanged + = value ;
}
remove
{
_ collectionChanged - = value ;
if ( _ collectionChanged is null )
{
if ( _ inner is INotifyCollectionChanged incc )
{
incc . CollectionChanged - = OnCollectionChanged ;
}
}
}
}
/// <inheritdoc/>
/// <inheritdoc/>
public void Dispose ( )
public void Dispose ( )
{
{
if ( _ notifyCollectionChanged ! = null )
if ( _ inner is INotifyCollectionChanged incc )
{
{
_ notifyCollectionChanged . CollectionChanged - = OnCollectionChanged ;
incc . CollectionChanged - = OnCollectionChanged ;
}
}
}
}
@ -162,16 +187,7 @@ namespace Avalonia.Controls
protected void OnItemsSourceChanged ( NotifyCollectionChangedEventArgs args )
protected void OnItemsSourceChanged ( NotifyCollectionChangedEventArgs args )
{
{
CollectionChanged ? . Invoke ( this , args ) ;
_ collectionChanged ? . Invoke ( this , args ) ;
}
private void ListenToCollectionChanges ( )
{
if ( _ inner is INotifyCollectionChanged incc )
{
incc . CollectionChanged + = OnCollectionChanged ;
_ notifyCollectionChanged = incc ;
}
}
}
private void OnCollectionChanged ( object sender , NotifyCollectionChangedEventArgs e )
private void OnCollectionChanged ( object sender , NotifyCollectionChangedEventArgs e )