You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
5.2 KiB
C#
176 lines
5.2 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Specialized;
|
|
using System.ComponentModel;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Runtime.Serialization;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using System.Text;
|
|
|
|
namespace POSV.Common.Util
|
|
{
|
|
public delegate void CollectionChangedEventHander(object sender , CollectionChangedEventArgs e);
|
|
|
|
public enum CollectionChangedAction
|
|
{
|
|
None = -1,
|
|
Add = 0,
|
|
Remove = 1,
|
|
Reset = 2,
|
|
Replace = 3
|
|
}
|
|
|
|
public class CollectionChangedEventArgs : EventArgs
|
|
{
|
|
private readonly CollectionChangedAction _action;
|
|
private readonly object _newItem;
|
|
private readonly object _oldItem;
|
|
private readonly int _index = -1;
|
|
|
|
public CollectionChangedEventArgs(CollectionChangedAction action , object newItem , object oldItem , int index)
|
|
{
|
|
this._action = action;
|
|
this._newItem = newItem;
|
|
this._oldItem = oldItem;
|
|
|
|
this._index = index;
|
|
}
|
|
|
|
public CollectionChangedEventArgs(CollectionChangedAction action) : this(action , null, null , -1)
|
|
{
|
|
|
|
}
|
|
|
|
public CollectionChangedEventArgs(CollectionChangedAction action, object newItem , int index) : this(action , newItem , null , index)
|
|
{
|
|
|
|
}
|
|
|
|
public CollectionChangedAction Action
|
|
{
|
|
get
|
|
{
|
|
return this._action;
|
|
}
|
|
}
|
|
|
|
public object NewItem
|
|
{
|
|
get { return _newItem; }
|
|
}
|
|
|
|
public object OldItem
|
|
{
|
|
get { return _oldItem; }
|
|
}
|
|
|
|
public int Index
|
|
{
|
|
get { return _index; }
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class ObservableDictionary<TKey, TValue> : SortedDictionary<TKey , TValue>, ICloneable
|
|
{
|
|
public ObservableDictionary(){}
|
|
|
|
//public ObservableDictionary(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
|
|
//: base(info, context)
|
|
//{
|
|
|
|
//}
|
|
|
|
//public ObservableDictionary(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
|
|
//: base(info, context)
|
|
//{
|
|
|
|
//}
|
|
|
|
public event CollectionChangedEventHander CollectionChanged;
|
|
|
|
protected virtual void OnCollectionChanged(CollectionChangedEventArgs e)
|
|
{
|
|
CollectionChanged?.Invoke(this , e);
|
|
}
|
|
|
|
public new TValue this[TKey key]
|
|
{
|
|
get
|
|
{
|
|
return base[key];
|
|
}
|
|
set
|
|
{
|
|
|
|
TValue oldValue;
|
|
bool exist = base.TryGetValue(key , out oldValue);
|
|
var oldItem = new KeyValuePair<TKey , TValue>(key , oldValue);
|
|
base[key] = value;
|
|
var newItem = new KeyValuePair<TKey , TValue>(key , value);
|
|
if (exist)
|
|
{
|
|
this.OnCollectionChanged(new CollectionChangedEventArgs(CollectionChangedAction.Replace , newItem , oldItem , base.Keys.ToList().IndexOf(key)));
|
|
}
|
|
else
|
|
{
|
|
this.OnCollectionChanged(new CollectionChangedEventArgs(CollectionChangedAction.Add , newItem , base.Keys.ToList().IndexOf(key)));
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
public new void Add(TKey key , TValue value)
|
|
{
|
|
if (!base.ContainsKey(key))
|
|
{
|
|
var item = new KeyValuePair<TKey , TValue>(key , value);
|
|
base.Add(key , value);
|
|
this.OnCollectionChanged(new CollectionChangedEventArgs(CollectionChangedAction.Add , item , base.Keys.ToList().IndexOf(key)));
|
|
}
|
|
}
|
|
|
|
public void SaveOrUpdateNoChanged(TKey key , TValue value)
|
|
{
|
|
TValue oldValue;
|
|
base.TryGetValue(key , out oldValue);
|
|
base[key] = value;
|
|
|
|
Console.WriteLine(">>>>>>>>>>>>>>>>>>>>");
|
|
}
|
|
|
|
public new bool Remove(TKey key)
|
|
{
|
|
TValue value;
|
|
if (base.TryGetValue(key , out value))
|
|
{
|
|
var item = new KeyValuePair<TKey , TValue>(key , base[key]);
|
|
bool result = base.Remove(key);
|
|
OnCollectionChanged(new CollectionChangedEventArgs(CollectionChangedAction.Remove , item , base.Keys.ToList().IndexOf(key)));
|
|
return result;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public new void Clear()
|
|
{
|
|
OnCollectionChanged(new CollectionChangedEventArgs(CollectionChangedAction.Reset));
|
|
base.Clear();
|
|
}
|
|
|
|
public object Clone()
|
|
{
|
|
using (Stream stream = new MemoryStream())
|
|
{
|
|
BinaryFormatter formatter = new BinaryFormatter(null, new System.Runtime.Serialization.StreamingContext(System.Runtime.Serialization.StreamingContextStates.Clone));
|
|
formatter.Serialize(stream, this);
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
return formatter.Deserialize(stream);
|
|
}
|
|
}
|
|
}
|
|
}
|