Advertisement
gajda-ltd

DispatcherObservableCollection

Feb 3rd, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.78 KB | None | 0 0
  1. namespace Core
  2. {
  3.     using System;
  4.     using System.Collections.ObjectModel;
  5.     using System.Collections.Specialized;
  6.     using System.ComponentModel;
  7.     using System.Windows.Threading;
  8.  
  9.     /// <summary>  
  10.     /// This class is an Observable Collection which invokes automatically.  
  11.     /// This means that any change will be done in the right thread.  
  12.     /// </summary>  
  13.     /// <typeparam name="T">The type of the elements</typeparam>  
  14.     public class DispatcherObservableCollection<T> : ObservableCollection<T>
  15.     {
  16.         /// <summary>  
  17.         /// The default constructor of the ObservableCollection  
  18.         /// </summary>  
  19.         public DispatcherObservableCollection()
  20.         {
  21.             //Assign the current Dispatcher (owner of the collection)  
  22.             this.currentDispatcher = Dispatcher.CurrentDispatcher;
  23.         }
  24.  
  25.         private readonly Dispatcher currentDispatcher;
  26.  
  27.         /// <summary>  
  28.         /// Executes this action in the right thread  
  29.         /// </summary>  
  30.         ///<param name="action">The action which should be executed</param>  
  31.         private void DoDispatchedAction(Action action)
  32.         {
  33.             if (this.currentDispatcher.CheckAccess())
  34.             {
  35.                 action.Invoke();
  36.             }
  37.             else
  38.             {
  39.                 this.currentDispatcher.Invoke(DispatcherPriority.DataBind, action);
  40.             }
  41.         }
  42.  
  43.         /// <summary>  
  44.         /// Clears all items  
  45.         /// </summary>  
  46.         protected override void ClearItems()
  47.         {
  48.             this.DoDispatchedAction(this.BaseClearItems);
  49.         }
  50.  
  51.         private void BaseClearItems()
  52.         {
  53.             base.ClearItems();
  54.         }
  55.  
  56.         /// <summary>  
  57.         /// Inserts a item at the specified index  
  58.         /// </summary>  
  59.         ///<param name="index">The index where the item should be inserted</param>  
  60.         ///<param name="item">The item which should be inserted</param>  
  61.         protected override void InsertItem(int index, T item)
  62.         {
  63.             this.DoDispatchedAction(() => this.BaseInsertItem(index, item));
  64.         }
  65.  
  66.         private void BaseInsertItem(int index, T item)
  67.         {
  68.             base.InsertItem(index, item);
  69.         }
  70.  
  71.         /// <summary>  
  72.         /// Moves an item from one index to another  
  73.         /// </summary>  
  74.         ///<param name="oldIndex">The index of the item which should be moved</param>  
  75.         ///<param name="newIndex">The index where the item should be moved</param>  
  76.         protected override void MoveItem(int oldIndex, int newIndex)
  77.         {
  78.             this.DoDispatchedAction(() => this.BaseMoveItem(oldIndex, newIndex));
  79.         }
  80.  
  81.         private void BaseMoveItem(int oldIndex, int newIndex)
  82.         {
  83.             base.MoveItem(oldIndex, newIndex);
  84.         }
  85.  
  86.         /// <summary>  
  87.         /// Removes the item at the specified index  
  88.         /// </summary>  
  89.         ///<param name="index">The index of the item which should be removed</param>  
  90.         protected override void RemoveItem(int index)
  91.         {
  92.             this.DoDispatchedAction(() => this.BaseRemoveItem(index));
  93.         }
  94.  
  95.         private void BaseRemoveItem(int index)
  96.         {
  97.             base.RemoveItem(index);
  98.         }
  99.  
  100.         /// <summary>  
  101.         /// Sets the item at the specified index  
  102.         /// </summary>  
  103.         ///<param name="index">The index which should be set</param>  
  104.         ///<param name="item">The new item</param>  
  105.         protected override void SetItem(int index, T item)
  106.         {
  107.             this.DoDispatchedAction(() => this.BaseSetItem(index, item));
  108.         }
  109.  
  110.         private void BaseSetItem(int index, T item)
  111.         {
  112.             base.SetItem(index, item);
  113.         }
  114.  
  115.         /// <summary>  
  116.         /// Fires the CollectionChanged Event  
  117.         /// </summary>  
  118.         ///<param name="args">The additional arguments of the event</param>  
  119.         protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
  120.         {
  121.             this.DoDispatchedAction(() => this.BaseOnCollectionChanged(args));
  122.         }
  123.  
  124.         private void BaseOnCollectionChanged(NotifyCollectionChangedEventArgs e)
  125.         {
  126.             base.OnCollectionChanged(e);
  127.         }
  128.  
  129.         /// <summary>  
  130.         /// Fires the PropertyChanged Event  
  131.         /// </summary>  
  132.         ///<param name="args">The additional arguments of the event</param>  
  133.         protected override void OnPropertyChanged(PropertyChangedEventArgs args)
  134.         {
  135.             this.DoDispatchedAction(() => this.BaseOnPropertyChanged(args));
  136.         }
  137.  
  138.         private void BaseOnPropertyChanged(PropertyChangedEventArgs args)
  139.         {
  140.             base.OnPropertyChanged(args);
  141.         }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement