Hi,
>I have already posted this on microsoft.public.dotnet.general, but without
>answer.
[quoted text clipped - 29 lines]
> synchronization
> automatically?
Your Custom Collection needs to implement IBindingList and return true for
SupportsChangeNofication. Then each object must inform the Custom
Collection if one if its property changed. Then the Custom Collection
fires the ListChanged event (available if you implement IBindingList).
The objects can also have a Changed event (EventHandler) for each property
but it is only used when binding directly to the object and not a list of.
Some relevant code:
public class CustomObject
{
public event EventHandler QuantityChanged;
private int quantity;
internal CustomCollection List;
[System.ComponentModel.Bindable(true)]
public int Quantity
{
get { return quantity; }
set
{
if (quantity != value)
{
quantity = value;
if (QuantityChanged != null)
QuantityChanged(this, EventArgs.Empty );
NotifyList();
}
}
private void NotifyList()
{
if ( List!=null )
List.ItemChanged( this );
}
}
//
// incomplete, only relevant parts are shown
//
public class CustomCollection : CollectionBase, IBindingList
{
public void Add( CustomObject o )
{
List.Add( o );
}
protected virtual void OnInsert( int index, object value )
{
CustomObject o = value as CustomObject;
if ( o == null )
throw new Exception("Insert CustomObjects only");
else
o.List = this;
}
protected virtual void OnInsertComplete( int index, object value )
{
OnListChanged( new ListChangedEventArgs(
ListChangedType.ItemAdded, index ) );
}
internal void ItemChanged( CustomObject o )
{
OnListChanged( new ListChangedEventArgs(
ListChangedType.ItemChanged,
List.IndexOf( o ) ));
}
}
protected virutal OnListChanged( ListChangedEventArgs e )
{
if ( ListChanged!=null )
ListChanged( this, e );
}
}
HTH,
Greetings
> I know that I can do something like that, but I want nicer solution
> (pseudo
[quoted text clipped - 13 lines]
>
> Sanjin
Sanjin - 12 Oct 2005 13:40 GMT
Thanks, that was what I was looking for.