views:

38

answers:

2

Hi All,

I have a (Image) User Control on a Winform ..
I want to bind this Control to a binding list such that whenever the (images) collection changes the (images showing on the ) control also change. (should reflect the images within the collection).
[something similar to an AsyncBinding List.] .

Similar to how .net controls use DataSource Property.

//[EDITED] need Binding list.

    BindingList<Image> _images = GetImages("folder_path");   
    ImageControl ctrl = new ImageControl();   
    ctrl.DataSource = _images; //something similar

How can i achieve the same?

EDIT2:
Basically I want to display Images in a List..Something similar to how picasa displays it.

image example

Thanks All

+1  A: 

You'll want to use a BindingList, rather than a List or Collection. The key difference is that the BindingList supports INotifyPropertyChanged, which is required to perform two-way databinding. Without that interface you can initially bind to the list, but changes to the list won't reflect in the UI.

STW
Oops i will edit my question. :)
Amitd
+1 .. re-edited my question . see Edit2 :)
Amitd
+1  A: 

As STW already mentioned you need a BindingList<T> so that a change within the list will be propagiated into the BindingSource.

The next step is how will your UserControl being notified about changes in the BindingSource. To solve this problem add this code as starting point to your UserControl.cs:

private BindingSource _DataSource;
[TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design")]
[Editor("System.Windows.Forms.Design.DataSourceListEditor, System.Design", typeof(UITypeEditor))]
[AttributeProvider(typeof(IListSource))]
public object DataSource
{
    get
    {
        return _DataSource;
    }
    set
    {
        //Detach from old DataSource
        if (_DataSource != null)
        {
            _DataSource.ListChanged -= _DataSource_ListChanged;
        }

        _DataSource = value as BindingSource;

        //Attach to new one
        if (_DataSource != null)
        {
            _DataSource.ListChanged += _DataSource_ListChanged;

            //ToDo: look for other (maybe usable) BindingSource events
            //http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource_events.aspx
        }
    }
}

void _DataSource_ListChanged(object sender, ListChangedEventArgs e)
{
    //ToDo: Reacht on specific list change
    switch (e.ListChangedType)
    {
        case ListChangedType.ItemAdded:
            break;
        case ListChangedType.ItemChanged:
            break;
        case ListChangedType.ItemDeleted:
            break;
        case ListChangedType.ItemMoved:
            break;
        case ListChangedType.PropertyDescriptorAdded:
            break;
        case ListChangedType.PropertyDescriptorChanged:
            break;
        case ListChangedType.PropertyDescriptorDeleted:
            break;
        case ListChangedType.Reset:
            break;
        default:
            break;
    }
}

With this you will be informed if anyone makes changes to the BindingSource itself (exchange the whole list against a new one) or on the list of elements attached to the BindingSource.

Also you should really good test it, cause some controls have a little weird usage of lists (e.g. instead of changing some elements on a list, clear it and fill it up from scratch).

Maybe this Walkthrough from Microsoft can help, too. I didn't read it, but it looks promising.

Oliver
+1 thx i need something similar :) .please see "Edit2" also :)
Amitd