mirror of
https://github.com/colhountech/DarkUI.Net5.git
synced 2025-07-22 20:01:34 +03:00
Created ObservableList
Class used for notifying when an item is added/removed from a list without raising events for things like index changes.
This commit is contained in:
parent
8408c47a3f
commit
217058af72
93
DarkUI/Collections/ObservableList.cs
Normal file
93
DarkUI/Collections/ObservableList.cs
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace DarkUI
|
||||||
|
{
|
||||||
|
public class ObservableList<T> : List<T>, IDisposable
|
||||||
|
{
|
||||||
|
#region Field Region
|
||||||
|
|
||||||
|
private bool _disposed;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Event Region
|
||||||
|
|
||||||
|
public event EventHandler<ObservableListModified<T>> ItemsAdded;
|
||||||
|
public event EventHandler<ObservableListModified<T>> ItemsRemoved;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Destructor Region
|
||||||
|
|
||||||
|
~ObservableList()
|
||||||
|
{
|
||||||
|
Dispose(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Dispose Region
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
Dispose(true);
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
if (!_disposed)
|
||||||
|
{
|
||||||
|
if (disposing)
|
||||||
|
{
|
||||||
|
// Release managed resources
|
||||||
|
}
|
||||||
|
|
||||||
|
// Release unmanaged resources.
|
||||||
|
// Set large fields to null.
|
||||||
|
if (ItemsAdded != null)
|
||||||
|
ItemsAdded = null;
|
||||||
|
|
||||||
|
if (ItemsRemoved != null)
|
||||||
|
ItemsRemoved = null;
|
||||||
|
|
||||||
|
// Set disposed flag
|
||||||
|
_disposed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Method Region
|
||||||
|
|
||||||
|
public new void Add(T item)
|
||||||
|
{
|
||||||
|
base.Add(item);
|
||||||
|
|
||||||
|
if (ItemsAdded != null)
|
||||||
|
ItemsAdded(this, new ObservableListModified<T>(new List<T> { item }));
|
||||||
|
}
|
||||||
|
|
||||||
|
public new void AddRange(IEnumerable<T> collection)
|
||||||
|
{
|
||||||
|
var list = collection.ToList();
|
||||||
|
|
||||||
|
base.AddRange(list);
|
||||||
|
|
||||||
|
if (ItemsAdded != null)
|
||||||
|
ItemsAdded(this, new ObservableListModified<T>(list));
|
||||||
|
}
|
||||||
|
|
||||||
|
public new void Remove(T item)
|
||||||
|
{
|
||||||
|
base.Remove(item);
|
||||||
|
|
||||||
|
if (ItemsRemoved != null)
|
||||||
|
ItemsRemoved(this, new ObservableListModified<T>(new List<T> { item }));
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
15
DarkUI/Collections/ObservableListModified.cs
Normal file
15
DarkUI/Collections/ObservableListModified.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace DarkUI
|
||||||
|
{
|
||||||
|
public class ObservableListModified<T> : EventArgs
|
||||||
|
{
|
||||||
|
public IEnumerable<T> Items { get; private set; }
|
||||||
|
|
||||||
|
public ObservableListModified(IEnumerable<T> items)
|
||||||
|
{
|
||||||
|
Items = items;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -38,6 +38,8 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="Collections\ObservableList.cs" />
|
||||||
|
<Compile Include="Collections\ObservableListModified.cs" />
|
||||||
<Compile Include="Config\Colors.cs" />
|
<Compile Include="Config\Colors.cs" />
|
||||||
<Compile Include="Config\Consts.cs" />
|
<Compile Include="Config\Consts.cs" />
|
||||||
<Compile Include="Config\Enums.cs" />
|
<Compile Include="Config\Enums.cs" />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user