Skip to content

Instantly share code, notes, and snippets.

@tmyt
Last active October 24, 2015 19:44
Show Gist options
  • Save tmyt/d76ce5895efc9dbff1b2 to your computer and use it in GitHub Desktop.
Save tmyt/d76ce5895efc9dbff1b2 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
namespace XamlPullToRefresh
{
public class ObservableCollectionEx<T> : ObservableCollection<T>
{
private bool? _hasPropertyChanged;
private bool HasPropertyChanged()
{
return _hasPropertyChanged ??
(_hasPropertyChanged = typeof(T).GetInterfaces().Contains(typeof(INotifyPropertyChanged))).Value;
}
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
base.OnCollectionChanged(e);
if (!(HasPropertyChanged())) return;
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
HandleAdd(e.NewItems);
break;
case NotifyCollectionChangedAction.Remove:
HandleRemove(e.OldItems);
break;
case NotifyCollectionChangedAction.Replace:
case NotifyCollectionChangedAction.Reset:
HandleRemove(e.OldItems);
HandleAdd(e.NewItems);
break;
}
}
private void HandleAdd(IList newItems)
{
foreach (var t in newItems.OfType<INotifyPropertyChanged>())
{
t.PropertyChanged += ChildrenPropertyChanged;
}
}
private void HandleRemove(IList newItems)
{
foreach (var t in newItems.OfType<INotifyPropertyChanged>())
{
t.PropertyChanged -= ChildrenPropertyChanged;
}
}
private void ChildrenPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
{
// どこに通知しよっか
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment