1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
public class ConnectedCollectionBase<T> : BindingList<T>, IMessageSender<T> where T:IRuleable<T>,IBindableObject
{
private Guid? toto = Guid.Empty;
public Guid? Toto
{
get
{
if (toto.Equals(Guid.Empty))
toto= Guid.NewGuid();
return toto;
}
}
/// <summary>
/// Event to fire a message to a GUI.
/// </summary>
public event EventHandler<MessageEventArgs<T>> MessageSend;
/// <summary>
/// Method implemented to raise the MessageSend event.
/// </summary>
/// <param name="e">The argument passed when firing the event.</param>
public virtual void OnMessageSend(MessageEventArgs<T> e)
{
if (MessageSend != null)
MessageSend(this, e);
}
/// <summary>
/// Removes an item from the collection. The method is called when deleting an item from a binded control for instance.
/// </summary>
/// <remarks>The item is only removed if the call to the delete method of the underlying object succeeds.</remarks>
/// <param name="index">The index of the item that is deleted.</param>
protected override void RemoveItem(int index)
{
if (this.Items[index].Delete(true))
{
IMessageSender<T> rules = this.Items[index].Rules as IMessageSender<T>;
if (rules != null)
{
rules.MessageSend -= new EventHandler<MessageEventArgs<T>>(ObjectRules_SendMessage);
base.RemoveItem(index);
}
}
}
/// <summary>
/// Method called when a property of an item of the underlying connection occures.
/// </summary>
/// <param name="e">Parameter to know which action was made.</param>
///<remarks>The items are inserted only when an item is added and can be inserted in the underlying store.
/// If the object is binded to a table and that constraints are not respected, the object is not added until all constraints can apply.
/// </remarks>
protected override void OnListChanged(ListChangedEventArgs e)
{
if (e.ListChangedType == ListChangedType.ItemAdded)
{
_adding = true;
IMessageSender<T> obj = this.Items[e.NewIndex] as BindableObject<T>;
if (obj != null)
{
obj.MessageSend += new EventHandler<MessageEventArgs<T>>(obj_MessageSend);
}
}
}
void obj_MessageSend(object sender, MessageEventArgs<T> e)
{
this.OnMessageSend(e);
}
/// <summary>
/// .ctor
/// </summary>
public ConnectedCollectionBase():base(new List<T>())
{
}
/// <summary>
/// .ctor
/// </summary>
/// <param name="col">The base collection that is used to fill the inner collection.</param>
public ConnectedCollectionBase(IList<T> col)
: base(col)
{
}
} |
Partager