Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 3.26 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Lock / Unlock ObservableCollection<T>
  2. public class ReadOnlyObservableCollection<T> : ObservableCollection<T>
  3. {
  4.     // method overrides with conditional logic to allow/deny changes
  5. }
  6.        
  7. public interface ILockable
  8. {
  9.     bool Locked { get; set; }
  10. }
  11.  
  12. public class LockableObservableCollection<T> : ICollection<T>, INotifyCollectionChanged, INotifyPropertyChanged, ILockable
  13.     where T: ILockable
  14. {
  15.     protected ObservableCollection<T> Collection { get; set; }
  16.  
  17.     public LockableObservableCollection()
  18.     {
  19.         Collection = new ObservableCollection<T>();
  20.         Collection.CollectionChanged += new NotifyCollectionChangedEventHandler(Collection_CollectionChanged);
  21.         ((INotifyPropertyChanged)Collection).PropertyChanged +=
  22.             new PropertyChangedEventHandler(LockableObservableCollection_PropertyChanged);
  23.     }
  24.  
  25.     void LockableObservableCollection_PropertyChanged(object sender, PropertyChangedEventArgs e)
  26.     {
  27.         if (PropertyChanged != null) PropertyChanged(this, e);
  28.     }
  29.  
  30.     void Collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  31.     {
  32.         if (CollectionChanged != null) CollectionChanged(this, e);
  33.     }
  34.  
  35.     public T Item(int index)
  36.     {
  37.         return Collection[index];
  38.     }
  39.  
  40.     #region ICollection<T>
  41.  
  42.     public void Add(T item)
  43.     {
  44.         if (Locked) throw new Exception("Collection is locked.");
  45.  
  46.         Collection.Add(item);
  47.     }
  48.  
  49.     public void Clear()
  50.     {
  51.         if (Locked) throw new Exception("Collection is locked.");
  52.  
  53.         Collection.Clear();
  54.     }
  55.  
  56.     public bool Contains(T item)
  57.     {
  58.         return Collection.Contains(item);
  59.     }
  60.  
  61.     public void CopyTo(T[] array, int arrayIndex)
  62.     {
  63.         Collection.CopyTo(array, arrayIndex);
  64.     }
  65.  
  66.     public int Count
  67.     {
  68.         get { return Collection.Count; }
  69.     }
  70.  
  71.     public bool IsReadOnly
  72.     {
  73.         get { return Locked; }
  74.     }
  75.  
  76.     public bool Remove(T item)
  77.     {
  78.         if (Locked) throw new Exception("Collection is locked.");
  79.  
  80.         bool result = Collection.Remove(item);
  81.         return result;
  82.     }
  83.  
  84.     public IEnumerator<T> GetEnumerator()
  85.     {
  86.         return Collection.GetEnumerator();
  87.     }
  88.  
  89.     System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  90.     {
  91.         return GetEnumerator();
  92.     }
  93.     #endregion
  94.  
  95.     #region INotifyCollectionChanged
  96.     public event NotifyCollectionChangedEventHandler CollectionChanged;
  97.     #endregion
  98.  
  99.     #region IPropertyChanged
  100.     public event PropertyChangedEventHandler PropertyChanged;
  101.     #endregion
  102.  
  103.     private bool locked;
  104.     public bool Locked
  105.     {
  106.         get
  107.         {
  108.             return locked;
  109.         }
  110.         set
  111.         {
  112.             if (locked != value)
  113.             {
  114.                 locked = value;
  115.                 foreach (T t in Collection)
  116.                 {
  117.                     t.Locked = value;
  118.                 }
  119.                 if (PropertyChanged != null)
  120.                 {
  121.                     PropertyChanged(this, new PropertyChangedEventArgs("Locked"));
  122.                 }
  123.  
  124.             }
  125.         }
  126.     }
  127. }
  128.        
  129. private string text;
  130.  
  131. public string Text
  132. {
  133.     get { return text; }
  134.     set
  135.     {
  136.         if (text != value)
  137.         {
  138.             if (Locked) throw new Exception("This item is locked to prevent changes.");
  139.             text = value;
  140.         }
  141.     }
  142. }