diff --git a/MSEssentials.UI.Common.MVVM/ObservableDictionary.cs b/MSEssentials.UI.Common.MVVM/ObservableDictionary.cs index f6f6c8c..9061410 100644 --- a/MSEssentials.UI.Common.MVVM/ObservableDictionary.cs +++ b/MSEssentials.UI.Common.MVVM/ObservableDictionary.cs @@ -1,37 +1,18 @@ -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; +using System.Collections.ObjectModel; using System.Diagnostics.CodeAnalysis; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace MSEssentials.UI.Common.MVVM { public class ObservableDictionary : ObservableCollection>, IDictionary, IEnumerable> { - #region PROPERTIES - public ICollection Keys => Items.Select(p => p.Key).ToList(); public ICollection Values => Items.Select(p => p.Value).ToList(); public bool IsReadOnly => false; - #endregion - - - - #region INDEXERS - + public TValue this[TKey key] { - get - { - if (!TryGetValue(key, out TValue result)) - { - throw new ArgumentException("Key not found"); - } - return result; - } + get => !TryGetValue(key, out TValue result) ? throw new ArgumentException("Key not found") : result; set { if (ContainsKey(key)) @@ -45,16 +26,11 @@ namespace MSEssentials.UI.Common.MVVM } } - #endregion - - - - #region CONSTRUCTORS - - public ObservableDictionary() : base() + + public ObservableDictionary() { } - public ObservableDictionary(IDictionary dictionary) : base() + public ObservableDictionary(IDictionary dictionary) { foreach (KeyValuePair pair in dictionary) { @@ -62,12 +38,8 @@ namespace MSEssentials.UI.Common.MVVM } } - #endregion - - - - #region PUBLIC METHODS - + + public void Add(KeyValuePair item) => Add(item.Key, item.Value); public void Add(TKey key, TValue value) { if (ContainsKey(key)) @@ -77,8 +49,6 @@ namespace MSEssentials.UI.Common.MVVM Add(new ObservableKeyValuePair(key, value)); } - public void Add(KeyValuePair item) => Add(item.Key, item.Value); - public bool Contains(KeyValuePair item) { ObservableKeyValuePair pair = GetKeyValuePairByTheKey(item.Key); @@ -96,20 +66,8 @@ namespace MSEssentials.UI.Common.MVVM return !Equals(default(ObservableKeyValuePair), pair); } - public void CopyTo(KeyValuePair[] array, int arrayIndex) - { + public void CopyTo(KeyValuePair[] array, int arrayIndex) => throw new NotImplementedException(); - } - - public bool Remove(TKey key) - { - List> remove = ((ObservableCollection>)this).Where(pair => Equals(key, pair.Key)).ToList(); - foreach (ObservableKeyValuePair pair in remove) - { - Remove(pair); - } - return remove.Count > 0; - } public bool Remove(KeyValuePair item) { @@ -124,6 +82,16 @@ namespace MSEssentials.UI.Common.MVVM } return Remove(pair); } + + public bool Remove(TKey key) + { + List> remove = ((ObservableCollection>)this).Where(pair => Equals(key, pair.Key)).ToList(); + foreach (ObservableKeyValuePair pair in remove) + { + Remove(pair); + } + return remove.Count > 0; + } public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) { @@ -137,16 +105,11 @@ namespace MSEssentials.UI.Common.MVVM return true; } - IEnumerator> IEnumerable>.GetEnumerator() => ((ObservableCollection>)this).Select(i => new KeyValuePair(i.Key, i.Value)).GetEnumerator(); + IEnumerator> IEnumerable>.GetEnumerator() => + ((ObservableCollection>)this).Select(i => new KeyValuePair(i.Key, i.Value)).GetEnumerator(); - #endregion - - - - #region PRIVATE METHODS - - private ObservableKeyValuePair GetKeyValuePairByTheKey(TKey key) => ((ObservableCollection>)this).FirstOrDefault(i => i.Key.Equals(key)); - - #endregion + + private ObservableKeyValuePair GetKeyValuePairByTheKey(TKey key) => + ((ObservableCollection>)this).FirstOrDefault(i => i.Key.Equals(key)); } } diff --git a/MSEssentials.UI.Common.MVVM/ObservableKeyValuePair.cs b/MSEssentials.UI.Common.MVVM/ObservableKeyValuePair.cs index 581ef87..8ab991c 100644 --- a/MSEssentials.UI.Common.MVVM/ObservableKeyValuePair.cs +++ b/MSEssentials.UI.Common.MVVM/ObservableKeyValuePair.cs @@ -9,17 +9,10 @@ namespace MSEssentials.UI.Common.MVVM { public class ObservableKeyValuePair : INotifyPropertyChanged { - #region FIELDS - private TKey _key; private TValue _value; - #endregion - - - - #region PROPERTIES - + public TKey Key { get => _key; @@ -38,12 +31,7 @@ namespace MSEssentials.UI.Common.MVVM NotifyPropertyChanged(nameof(Value)); } } - - #endregion - - - - #region CONSTRUCTORS + public ObservableKeyValuePair() : this(default, default) { } @@ -54,22 +42,10 @@ namespace MSEssentials.UI.Common.MVVM _value = value; } - #endregion - - - - #region PRIVATE METHODS - + private void NotifyPropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); - #endregion - - - - #region EVENTS - + public event PropertyChangedEventHandler? PropertyChanged; - - #endregion } }