Changeset 2424


Ignore:
Timestamp:
10/09/09 16:28:42 (13 years ago)
Author:
mkommend
Message:

changed ObservableCollection to implement INotifyCollectionChanged
and added ObservableList in branch mainform refactoring (ticket #779)

Location:
branches/Mainform refactoring/HeuristicLab.Common/3.2
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • branches/Mainform refactoring/HeuristicLab.Common/3.2/HeuristicLab.Common-3.2.csproj

    r2371 r2424  
    7878    <Reference Include="System.Data" />
    7979    <Reference Include="System.Xml" />
     80    <Reference Include="WindowsBase">
     81      <RequiredTargetFramework>3.0</RequiredTargetFramework>
     82    </Reference>
    8083  </ItemGroup>
    8184  <ItemGroup>
     85    <Compile Include="ObservableList.cs" />
    8286    <Compile Include="DoubleExtensions.cs">
    8387      <SubType>Code</SubType>
  • branches/Mainform refactoring/HeuristicLab.Common/3.2/ObservableCollection.cs

    r2323 r2424  
    2323using System.Linq;
    2424using System.Text;
     25using System.Collections.Specialized;
    2526
    2627namespace HeuristicLab.Common {
    27   public class ObservableCollection<T> : ICollection<T> {
    28     private List<T> list;
     28  public class ObservableCollection<T> : ICollection<T>, INotifyCollectionChanged {
     29    protected List<T> list;
    2930
    3031    public ObservableCollection() {
     
    3334
    3435    #region Events
    35     public event EventHandler CollectionCleared;
    36     protected void FireCollectionCleared() {
    37       OnCollectionCleared();
    38     }
    39     protected virtual void OnCollectionCleared() {
    40       if (CollectionCleared != null)
    41         CollectionCleared(this, new EventArgs());
     36    public event NotifyCollectionChangedEventHandler CollectionChanged;
     37
     38    protected void FireCollectionChanged(NotifyCollectionChangedEventArgs e) {
     39      if (CollectionChanged != null) {
     40        CollectionChanged(this, e);
     41      }
    4242    }
    4343
    44     public event EventHandler<EnumerableEventArgs<T>> ItemsAdded;
    45     protected void FireItemsAdded(IEnumerable<T> addedItems) {
    46       OnItemsAdded(addedItems);
    47     }
    48     protected virtual void OnItemsAdded(IEnumerable<T> addedItems) {
    49       if (ItemsAdded != null)
    50         ItemsAdded(this, new EnumerableEventArgs<T>(addedItems));
     44    protected virtual void FireCollectionCleared() {
     45        NotifyCollectionChangedEventArgs e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
     46        FireCollectionChanged( e);
    5147    }
    5248
    53     public event EventHandler<EnumerableEventArgs<T>> ItemsRemoved;
    54     protected void FireItemsRemoved(IEnumerable<T> removedItems) {
    55       OnItemsRemoved(removedItems);
     49    protected virtual void FireItemsAdded(IEnumerable<T> addedItems, int index) {
     50        NotifyCollectionChangedEventArgs e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, addedItems.ToList(), index);
     51        FireCollectionChanged(e);
    5652    }
    57     protected virtual void OnItemsRemoved(IEnumerable<T> removedItems) {
    58       if (ItemsRemoved != null)
    59         ItemsRemoved(this, new EnumerableEventArgs<T>(removedItems));
     53
     54    protected virtual void FireItemsRemoved(IEnumerable<T> removedItems) {
     55        NotifyCollectionChangedEventArgs e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, removedItems.ToList());
     56        FireCollectionChanged(e);
    6057    }
    6158    #endregion
     
    6360    #region ICollection<T> Members
    6461    public void Add(T item) {
     62      int index = this.list.Count - 1;
    6563      this.list.Add(item);
    66       FireItemsAdded(new List<T> { item });
     64      FireItemsAdded(new List<T> { item }, index);
    6765    }
    6866
    6967    public void AddRange(IEnumerable<T> items) {
     68      int index = this.list.Count - 1;
    7069      this.list.AddRange(items);
    71       FireItemsAdded(items);
     70      FireItemsAdded(items, index);
    7271    }
    7372
     
    125124
    126125    #endregion
     126
     127
    127128  }
    128129}
Note: See TracChangeset for help on using the changeset viewer.